//基本思路：排列组合
//期望得分：4 + 12 = 16 pts 
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const long long N = 100010, mod = 1000000007;
void read(long long &x);
vector <pair <long long, long long> > e[N];
vector <long long> edge[N];
long long n, T, c, k, a[N], ans, l[N], p;
long long dfs(long long x, long long s)
{
	if (s > n)
		return 1;
	long long coun = 0;
	for (auto i : edge[x])
		if (!l[i])
		{
			l[i] = 1;
			(coun += dfs(i, s + 1)) %= mod;
			l[i] = 0;
		}
	return coun;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	read(c);
	read(T);
	while(T--)
	{
		read(n);
		read(k);
		for (int i = 1; i <= n; i++)
			e[i].clear();
		for (long long i = 1, u, v; i < n; i++)
		{
			read(u);
			read(v);
			e[u].push_back(make_pair(v, i));
			e[v].push_back(make_pair(u, i));
		}
		for (long long i = 1; i <= k; i++)
			read(a[i]);
		if (c == 18)
		{
			printf("1\n");
			continue;
		}
		if (c >= 19 && c <= 21)
		{
			p = ans = 1;
			for (long long i = 2; i < n - 2; i++)
				(p *= i) %= mod;
			ans = (p * (n - 2)) % mod;
			(ans *= k) %= mod;
			for (int i = 2; i <= k; i++)
				(ans -= p * (i - 1) % mod - mod) %= mod;
			printf("%lld\n", ans);
			continue;
		}
		for (int i = 1; i <= n; i++)
		{
			for (auto x : e[i])
			{
				for (auto y : e[i])
				{
					if (x.fi == y.fi)
						continue;
					edge[x.se].push_back(y.se);
				}
			}
		}
		l[a[1]] = 1;
		printf("%lld\n", dfs(a[1], 1) % mod);
		l[a[1]] = 0;
	}
	return 0;
}
void read(long long &x)
{
	x = 0;
	long long t = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if (c == '-')
			t = -t;
		c = getchar();
	}
	while(isdigit(c))
	{
		x *= 10;
		x += (c - '0');
		c = getchar();
	}
	x *= t;
	return;
}
