#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10,mod=1e9+7;
int c;
ll n,frac[N],rt,d[N],K,vis[N];
struct edge{
	int u,v;
}e[N];
int fst[N],to[N<<1],nxt[N<<1],ecnt;
ll qpow(ll x,ll y){
	ll ret=1;
	while(y){
		if(y&1)ret=ret*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ret;
}
void solve(){
	cin>>n>>K;
	for(int i=1;i<=n;i++)d[i]=0;
	frac[0]=1;
	for(int i=1;i<=n;i++)frac[i]=frac[i-1]*i%mod;
	for(int i=1;i<n;i++){
		scanf("%d%d",&e[i].u,&e[i].v);
		d[e[i].u]++,d[e[i].v]++;
	}
	if(c==18){
		for(int i=1,x;i<=K;i++)scanf("%d",&x);
		cout<<"1\n";
		return;
	}
	if(c>=19&&c<=21){
		for(int i=1,x;i<=K;i++)scanf("%d",&x);
		ll ans=0;
		for(int i=1;i<=K;i++)
			ans=(ans+frac[n-2]*(n-i)%mod)%mod;
		cout<<ans%mod<<"\n";
		return;
	}if(c<=10){
		for(int i=1,x;i<=K;i++)scanf("%d",&x);
		ll ans=1;
		for(int i=1;i<=n;i++){
			ans=ans*frac[d[i]-1]%mod;
		}
		cout<<ans%mod<<"\n";
	}
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int T;cin>>c>>T;
	while(T--)solve();
	return 0;
}
