#include<bits/stdc++.h>
using std::cin;
using std::cout;
using ll=long long;
const int N=1e5+10,mod=1e9+7;

ll fac[N+10]={1};
void init_fac(){
	for(int i=1;i<=N;++i)
		fac[i]=fac[i-1]*i%mod;
}

int c,t,n,k,cnt;
ll ans;
int dg[N],hd[N],to[N<<1],nt[N<<1];
int e[N<<1];
void add(int u,int v){
	++dg[u],++dg[v];
	to[++cnt]=v,nt[cnt]=hd[u],hd[u]=cnt;
	to[++cnt]=u,nt[cnt]=hd[v],hd[v]=cnt;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin.tie(0)->sync_with_stdio(0);
	init_fac();
	cin>>c>>t;
	while(t--){
		memset(hd,0,sizeof(hd));
		memset(dg,0,sizeof(dg));
		cin>>n>>k,ans=1,cnt=0;
		for(int i=1,u,v;i<n;++i)
			cin>>u>>v,add(u,v);
		for(int i=1;i<=k;++i)
			cin>>e[i];
		for(int i=1;i<=n;++i)
			if(dg[i]>2)
				ans=ans*fac[dg[i]-1]%mod;
		cout<<ans<<'\n';
	}
	return 0;
}
