#include<bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int c,T,n,k,u[100001],v[100001],d[100001],fr[100001];
long long jc[100001],ans;
bool tag[100001];
vector<int>e[100001];
inline int read(){
	int num = 0;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' && c <= '9') num = num * 10 + c - '0',c = getchar();
	return num; 
}
inline void solveA(){
	T = read();
	while(T --) printf("1\n");
	return;
}
inline void solveB(){
	T = read();
	while(T --){
		long long ans = 0ll,jc = 1ll;//(n-3)!
		n = read(),k = read();
		for(int i = 2;i <= n - 3;i ++){
			jc = jc * i % MOD;
		}
		for(int i = 1;i < n;i ++){
			u[i] = read(),v[i] = read();
		}
		for(int i = 1;i <= k;i ++){
			c = read();
			ans = (ans + jc * (n - 2 - i + 1) % MOD) % MOD; 
		}
		printf("%lld\n",ans);
	} 
	return;
}
inline void solve1(){
	T = read();
	jc[0] = 1;
	for(int i = 1;i <= 100000;i ++) jc[i] = jc[i - 1] * i % MOD;
	while(T --){
		long long ans = 1ll;
		n = read(),k = read(); 
		for(int i = 1;i <= n;i ++) e[i].clear();
		for(int i = 1;i < n;i ++){
			u[i] = read(),v[i] = read();
			e[u[i]].push_back(v[i]),e[v[i]].push_back(u[i]);
		}
		k = read();
		for(int i = 1;i <= n;i ++) ans = ans * jc[e[i].size() - 1] % MOD;
		printf("%lld\n",ans);
	}
	return;
}
void sch(int u,int fa){
	if(fa != fr[u]){
		fr[u] = fa;
		d[u] ++;
	}
	for(auto v : e[u]){
		if(v == fa) continue;
		sch(v,u);
	}
	return;
}
inline void solve(){
	T = read();
	jc[0] = 1;
	for(int i = 1;i <= 100000;i ++) jc[i] = jc[i - 1] * i % MOD;
	while(T --){
		ans = 1ll;
		n = read(),k = read();
		memset(d,0,sizeof(d));
		memset(tag,0,sizeof(tag));
		memset(fr,0,sizeof(fr));
		for(int i = 1;i <= n;i ++) e[i].clear();
		for(int i = 1;i < n;i ++){
			u[i] = read(),v[i] = read();
			e[u[i]].push_back(v[i]),e[v[i]].push_back(u[i]);
		}
		for(int i = 1;i <= k;i ++){
			c = read();
			sch(u[c],v[c]);
			sch(v[c],u[c]); 
		}
		for(int i = 1;i <= n;i ++){
			if(d[i] == 1){
				ans = ans * jc[e[i].size() - 1] % MOD;
			}
			else if(d[i] == 2){
				if(e[i].size() == 1){
					continue;
				}
				ans = ans * ((2 * jc[e[i].size() - 1] - jc[e[i].size() - 2] + MOD) % MOD) % MOD;
			}
		}
		printf("%lld\n",ans);
	}
	return;
}
int main(){      
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	c = read();
	if(c == 18){
		solveA();
	}
	else if(c >= 19 && c <= 21){
		solveB();
	}
	else if(c <= 6){
		solve1();
	}
	else{
		solve();
	}
	return 0;
}





//11:20 WC 
//11:31 finish AB 0->16 pts
//12:01 decide to solve the fist 6 tests.
//12:10 1~6 finish but WA
//12:15 发现错误是因为没有读入关键边 
//12:16 finish 1~6, 16->40pts
//12:20 疑似想到正解 
//12:26 发现假了 
//12:34 发现没有完全假，貌似可以过 7~10 
//12:53 RE 放弃了 
