#include<bits/stdc++.h>
#define il inline
#define pii pair<int,int>
#define ll long long
#define int ll
#define fi first
#define se second
using namespace std;
#define gc() getchar()
il int rd(){
	int x=0,f=1;
	char ch=gc();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
	while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();
	return x*f;
}
const int N=1e6+10,mod=1e9+7;
/*

*/
int n,k,id;
vector<pii>e[N];
pii ed[N];
int c[N],d[N],pw[N],inv[N];
int ksm(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;y>>=1;
	}
	return s;
}
void Pre(int n){
	pw[0]=1;for(int i=1; i<=n; ++i)pw[i]=1ll*pw[i-1]*i%mod;
	inv[n]=ksm(pw[n],mod-2);for(int i=n-1; ~i; --i)inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
vector<int>E[N];
void add(int x,int F){
	for(auto [y,id]:e[x])if(id!=F){
		E[F].push_back(id);
		add(y,id);
	}
}
int dp[N];
void Dfs(int x,int F=0){
	dp[x]=1;
	for(auto y:E[x]){
		Dfs(y,x);
		dp[x]=1ll*dp[x]*dp[y]%mod;
	}
	dp[x]=1ll*dp[x]*(pw[E[x].size()])%mod;
}
void Main(){
	n=rd(),k=rd();
	for(int i=1; i<=n; ++i)d[i]=0,e[i].clear(),E[i].clear();
	for(int u,v,i=1; i<n; ++i){
		u=rd(),v=rd();
		ed[i]={u,v};
		e[u].push_back({v,i});
		e[v].push_back({u,i});
		++d[u],++d[v];
	}
	for(int i=1; i<=k; ++i)c[i]=rd();
	if(id==18){
		puts("1");
		return;
	}
	if(id==19){
		ll ans=1ll*pw[n-2]*k%mod;
		if(n>=3){
			ans=(ans-1ll*k*(k-1)/2*pw[n-3]%mod+mod)%mod;
		}
		printf("%lld\n",ans);
		return;
	}
	if(id<=6){
		int rt=c[1];
		add(ed[rt].fi,rt);
		add(ed[rt].se,rt);
		Dfs(rt);
		printf("%lld\n",dp[rt]);
		return;
	}
}
#undef int
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	Pre(1e6);
	id=rd();
	int T=rd();
	while(T--)Main();

	return 0;
}

