#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll c,T,n,k,s,t,po[10000001][2],ans,Por=1e9+7,mk1,mk2;
ll jc[10000001];
vector<int>e[1000001];
void read(ll &x){
	x=0;bool f=0;char c=getchar();
	while(c<48||c>57)f=c==45?1:0,c=getchar();
	while(c>47&&c<58)x=(x<<3)+(x<<1)+c-48,c=getchar();
	if(f)x=-x;
}
void dfs(int fa,int u){
	int v=e[u].size();
	ans=(ans+jc[v-1])%Por;
	for(int i=0;i<e[u].size();i++){
		v=e[u][i];
		if(v!=fa){
			dfs(u,v);
		}
	}
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	jc[1]=1;
	for(int i=2;i<=1e5;i++)
		jc[i]=jc[i-1]*i%Por;
read(c);read(T);while(T--){
	ans=0;mk1=0;mk2=0;
	read(n);read(k);
	for(int i=1;i<n;i++){
		read(s);read(t);
		e[s].push_back(t);
		e[t].push_back(s);
		po[i][0]=s;po[i][1]=t;
	}
	if(c==18){
		while(k--)
			read(s);
		printf("1\n");
	}else if(c>18&&c<23){
		for(int i=1;i<=k;i++)
			read(s);
		printf("%lld\n",jc[k-1]);
	}else {
		while(k--){
		read(s);
		dfs(po[s][1],po[s][0]);
		dfs(po[s][0],po[s][1]);
		}
		printf("%lld\n",ans);
	}
		
	
}return 0;
}
