#include <bits/stdc++.h>
#define int long long
using namespace std;

const int mod = 1e9 + 7;

int ccc, ttt, n, k, kk, h[100005], c[100005], ans, f[100005];

map<string, bool> mp;

bool v[100005];

struct node
{
	int a, b, n;
} d1[100005], d[100005];

void cun(int a, int b)
{
	d[++kk].a = a, d[kk].b = b;
	d[kk].n = h[a], h[a] = kk;
	return;
}

int jc(int x)
{
	int sum = 1;
	for (int i = 2; i <= x; i++)
	{
		sum *= i;
		sum %= mod;
	}
	return sum;
}

string pai(string s)
{
	int p[15];
	for (int i = 0; i < s.size(); i++)
	{
		p[i] = s[i];
	}
	sort(p, p + s.size());
	for (int i = 0; i < s.size(); i++)
	{
		s[i] = p[i];
	}
	return s;
}

void dfs(int x, int st, string ss)
{
	if (st >= n - 1)
	{
		ss = pai(ss);
		if (mp[ss] != 1)
		{
			ans++;
		}
		mp[ss] = 1;
		return;
	}
	int cnt = 0;
	for (int j = h[x]; j; j = d[j].n)
	{
		int b = d[j].b;
		if (v[b] != 1)
		{
			f[b] = x;
		}
		cnt += (v[b] != 1);
	}
	if (cnt == 0)
	{
		dfs(f[x], st, ss);
		return;
	}
	for (int j = h[x]; j; j = d[j].n)
	{
		int b = d[j].b;
		if (v[b] == 1)
		{
			continue;
		}
		f[b] = x;
		v[b] = 1;
		dfs(b, st + 1, ss + (char)('0' + (j + 1) / 2));
		v[b] = 0;
	}
}

signed main()
{
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	scanf("%lld%lld", &ccc, &ttt);
	while (ttt--)
	{
		memset(v, 0, sizeof(v));
		ans = 0;
		scanf("%lld%lld", &n, &k);
		for (int i = 1; i < n; i++)
		{
			scanf("%lld%lld", &d1[i].a, &d1[i].b);
		}
		for (int i = 1; i <= k; i++)
		{
			scanf("%lld", &c[i]);
		}
		if (ccc == 18)
		{
			printf("1\n");
			continue;
		}
		if (ccc >= 19 && ccc <= 21)
		{
			printf("%lld\n", jc(n - 1) % mod);
			continue;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				if (d1[i].a == d1[j].a || d1[i].a == d1[j].b || d1[i].b == d1[j].a || d1[i].b == d1[j].b)
				{
					cun(i, j);
					cun(j, i);
				}
			}
		}
		for (int i = 1; i <= k; i++)
		{
			string s = "";
			v[c[i]] = 1;
			dfs(c[i], 1, s);
			v[c[i]] = 0;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
