#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define int long long
#define endl "\n"
using namespace std;
const int N=1e5+5,mod=1e9+7;
int hea[N],ve[N<<1],nex[N<<1],cnt;
void add(int u,int v){
	ve[++cnt]=v,nex[cnt]=hea[u],hea[u]=cnt;
}
int xe[N],uu[N],vv[N],fac[N],siz[N];
//int dfs(int u,int fa){
//	int ans=fac[siz[u]-1];
////	cout<<u<<endl;
//	for(int i=hea[u];i;i=nex[i]){
//		int v=ve[i];
//		if(v==fa)continue;
//		ans=ans*dfs(v,u)%mod;
//	}
//	return ans;
//}
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin.tie(0);cout.tie(0);
	ios::sync_with_stdio(false);
	int id,t;
	cin>>id>>t;
	while(t--){
		int n,k;
		cin>>n>>k;
		fac[0]=1;
		for(int i=1;i<=n;i++)fac[i]=fac[i-1]*i%mod;
		memset(hea,0,sizeof(hea));cnt=0;
		memset(siz,0,sizeof(siz));
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			add(u,v);add(v,u);
			siz[u]++,siz[v]++;
			uu[i]=u,vv[i]=v;
		}
		for(int i=1;i<=k;i++)cin>>xe[i];
		if(19<=id && id<=21){
			int x=k*(k-1)/2;
			if(n==2){
				cout<<1<<endl;
				continue;
			}
			cout<<(k*fac[n-2]%mod-x*fac[n-3]%mod+mod)%mod<<endl;
		}
		else if(id==18)cout<<1<<endl;
		else{
			int ans=1;
			for(int i=1;i<=n;i++)ans=ans*fac[siz[i]-1]%mod;
			cout<<ans<<endl;
		}
	}
}
