#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, M = 2e5 + 10, K = 510, mod = 1e9 + 7;

int c, n, m;
int head[N], to[M], nxt[M], idx;
bool g[K][K];
bool st[N];
int w[N];
int cnt[N];

void add(int a, int b)
{
	to[idx] = b, nxt[idx] = head[a], head[a] = idx++;
}

bool check()
{
	for (int i = 1; i < n; i++)
	{
		cnt[i] = 0;
		for (int j = 1; j < n; j++)
			if (g[i][j]) cnt[i]++;
	}
	if (!st[w[1]]) return false;
	for (int i = 2; i < n; i++)
		for (int j = i - 1; j; j--)
			if ((j > 1 && cnt[w[j]] > 1) || (j == 1 && cnt[w[j]]))
			{
				if (!g[w[j]][w[i]]) return false;
				cnt[w[j]]--;
				break;
			}
	return true;
}

void bl()
{
	memset(g, 0, sizeof g);
	memset(st, 0, sizeof st);
	for (int i = 1; i <= n; i++)
	{
		for (int j = head[i]; ~j; j = nxt[j])
		{
			int v = to[j];
			for (int k = head[i]; ~k; k = nxt[k])
			{
				int _v = to[k];
				if (_v == v) continue;
				g[v][_v] = true;
			}
		}
	}
	for (int i = 0; i < m; i++)
	{
		int e;
		scanf("%d", &e);
		st[e] = true;
	}
	int res = 0;
	iota(w + 1, w + n, 1);
	do
	{
		if (check()) res++;
	}
	while (next_permutation(w + 1, w + n));
	printf("%d\n", res);
}

void sol()
{	
	if (c == 18)
	{
		puts("1");
		return;
	}
	memset(head, -1, sizeof head);
	idx = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; i++)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		add(a, i), add(b, i);
	}
	if (c < 19 || c > 21)
	{
		bl();
		return;
	}
	int res = 0, t = 1, _t = 1;
	if (c >= 19 && c <= 21)
	{
		for (int i = 2; i <= n - 3; i++) _t = 1ll * _t * i % mod;
		t = _t * (n - 2ll) % mod;
	}
	for (int i = 0; i < m; i++)
	{
		int e;
		scanf("%d", &e);
		if (c >= 19 && c <= 21) res = (res + t - 1ll * _t * i % mod + mod) % mod;
	}
	printf("%d\n", res);
}

signed main()
{
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	int T;
	scanf("%d%d", &c, &T);
	while (T--) sol();
	return 0;
}
