#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
const int inf = 1e9;
typedef long long ll;
const ll INF = 1e15;
int read() {
	int s = 0,f = 1;char ch = getchar();
	while (!isdigit(ch)) f = ch == '-' ? -1 : 1, ch = getchar();
	while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}
const int mod = 1e9 + 7;
int n,k;
struct edge {
	int v,nxt;
}ed[N * 2 + 10];
int head[N + 10],cnt;
void add(int u,int v) {
	ed[++cnt] = {v,head[u]};
	head[u] = cnt;
}
int a[N + 10],dp[N + 10];
pair<int,int> to[N + 10];
int jc[N + 10],tag[N + 10];
void dfs(int x,int fa) {
	dp[x] = 1;
	int ct = 0;
	for (int i = head[x];i;i = ed[i].nxt) {
		int v = ed[i].v;
		if (v == fa || tag[v]) continue;
		ct ++;
		dfs(v,x);
		dp[x] = 1ll * dp[x] * dp[v] % mod;
	}
	dp[x] = 1ll * dp[x] * jc[ct] % mod;
}
int st[N + 10],top,fl;
void dfs2(int x,int f,int y1,int y2) {
	if (fl) return;
	st[++top] = x;
	if (x == y1 || x == y2) {
		fl = 1;
		return;
	}
	for (int i = head[x];i;i = ed[i].nxt) {
		int v = ed[i].v;
		if (v == f) continue;
		dfs2(v,x,y1,y2);
	}
	if (fl) return;
	top --;
}
int getmod(int x) {
	return x - (x >= mod) * mod;
}
int du[N + 10],pre[N + 10],suf[N + 10],val[N + 10];
int main() {
//	system("fc traverse.out traverse10.ans");
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int id = read(),T = read();
	while (T -- ) {
		n = read(), k = read();
		memset(head,0,sizeof(int)*(n + 1));
		memset(du,0,sizeof(int)*(n + 1));
		cnt = 0;
		jc[0] = 1;
		for (int i = 1;i <= n;i ++ ) jc[i] = 1ll * jc[i - 1] * i % mod;
		for (int i = 1;i < n;i ++ ) {
			int u = read(),v = read();
			add(u,v), add(v,u);
			to[i] = {u,v};
			du[u] ++, du[v] ++;
		}
		for (int i = 1;i <= k;i ++ ) a[i] = read();
		for (int i = 1;i <= n;i ++ ) tag[i] = 0;
		if (id == 18) {
			printf("%lld\n",1);
			continue;
		}
		else if (id >= 19 && id <= 21) {
			int t = n - k - 1;
			int t2 = t * (t + 1ll) / 2 % mod;
			printf("%lld\n",1ll * (jc[n - 1] - 2ll * jc[n - 3] * (t2 - 1) % mod + mod) % mod);
			continue;
		}
		if (k == 1) {
			int x = to[a[1]].first,y = to[a[1]].second;
			dfs(x,y), dfs(y,x);
			printf("%lld\n",1ll * dp[x] * dp[y] % mod);
		}
		else {
//			cerr << 233;
			int x = to[a[1]].first,y = to[a[1]].second;
			int u = to[a[2]].first,v = to[a[2]].second;
//			cout << x << ' ' << y << ' ' << u << ' ' << v << endl;
			fl = top = 0;
			dfs2(x,y,u,v);
			int v1,v2;
			if (!fl) top = 0, dfs2(y,x,u,v);
//			assert(fl == 1);
			if (st[1] == x) dfs(y,x), v1 = dp[y];
			else dfs(x,y), v1 = dp[x];
			if (st[top] == u) dfs(v,u), v2 = dp[v];
			else dfs(u,v), v2 = dp[u];
			int ans = 1;
			tag[u] = tag[v] = tag[x] = tag[y] = 1;
			for (int i = 1;i <= top;i ++ ) tag[st[i]] = 1;
			pre[0] = suf[top + 1] = 1;
			for (int i = 1;i <= top;i ++ ) {
				dfs(st[i],0);
				val[i] = 2ll * jc[du[st[i]] - 1] % mod;
				for (int j = head[st[i]];j;j = ed[j].nxt) {
					int v = ed[j].v;
					if (!tag[v]) val[i] = 1ll * val[i] * dp[v] % mod;
				}
				ans = 1ll * ans * val[i] % mod;
				pre[i] = 1ll * pre[i - 1] * val[i] % mod;
			}
			for (int i = top;i;i -- ) suf[i] = 1ll * suf[i + 1] * val[i] % mod;
			for (int i = 1;i <= top;i ++ ) {
				int res = jc[du[st[i]] - 2] % mod;
				for (int j = head[st[i]];j;j = ed[j].nxt) {
					int v = ed[j].v;
					if (!tag[v]) res = 1ll * res * dp[v] % mod;
				}
				ans = (ans - 1ll * res * pre[i - 1] % mod * suf[i + 1] % mod + mod) % mod;
			}
			printf("%lld\n",1ll * ans * v1 % mod * v2 % mod);
		}
	}
	return 0;
}
/*
19 1
4 1
1 2
1 3
1 4
1
*/ 
