#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int mod=1e9+7;
int c,T,n,k;
int x[N],y[N],e[N];
vector<pair<int,int> >v[N];
long long ans;
bool vis[N];
int fa[N];
void dfs(int x,int y,int id,int gs){
	if(gs==n-1){
		ans++;
		return ;
	}
	for(int i=0;i<v[x].size();i++){
		if(!vis[v[x][i].second]){
			vis[v[x][i].second]=1;
			fa[v[x][i].second]=id;
			dfs(x,v[x][i].first,v[x][i].second,gs+1);
			vis[v[x][i].second]=0;
		}
	}
	for(int i=0;i<v[y].size();i++){
		if(!vis[v[y][i].second]){
			vis[v[y][i].second]=1;
			fa[v[x][i].second]=id;
			dfs(y,v[y][i].first,v[y][i].second,gs+1);
			vis[v[y][i].second]=0;
		}
	}
}
int read(){
	int s=0;
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') s=s*10+c-'0',c=getchar();
	return s;
}
int bj[N];
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>T;
	while(T--){
		cin>>n>>k;
		int flA=1,flB=1,cnt=0;
		long long sj=1;
		for(int i=1;i<n;i++){
			x[i]=read(),y[i]=read();
			if(x[i]!=i||y[i]!=i+1) flA=0;
			if(x[i]!=1||y[i]!=i+1) flB=0;
			v[x[i]].push_back({y[i],i});
			v[y[i]].push_back({x[i],i});
			if(i<=n-2) sj=sj*i%mod;
		}
		for(int i=1;i<=k;i++){
			e[i]=read();
			if(bj[e[i]]!=T){
				bj[e[i]]=T;
				cnt++;
			}
		}
		if(flA){
			printf("1\n");
			continue;
		}
		if(k==1){
			ans=0;
			dfs(x[e[1]],y[e[1]],e[1],1);
			printf("%lld\n",ans);
			continue;
		}
		if(flB){
			printf("%lld\n",sj*cnt%mod);
			continue;
		}
		if(k==2){
			ans=0;
			for(int i=1;i<=k;i++)
				dfs(x[e[1]],y[e[1]],e[1],1);
			printf("%lld\n",ans);
			continue;
		}
	}
	return 0;
}
/*
7 1
5 2
1 2
1 3
2 4
2 5
1 3
*/
