#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
int ce,zu,n,k,x,y,e[N];
const ll mod=1e9+7;
ll deg[N],jc[N],ans;
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&ce,&zu);
	jc[0]=1;
	for(int i=1;i<=100000;i++) jc[i]=jc[i-1]*i%mod;
	while(zu--){
		if(ce==18){
			printf("1\n");
			continue;
		} 
		ans=1;
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++) deg[i]=0;
		for(int i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			deg[x]++,deg[y]++;
		}
		for(int i=1;i<=k;i++) scanf("%d",&e[i]);
		for(int i=1;i<=n;i++) ans=ans*jc[deg[i]-1]%mod; 
		printf("%lld\n",ans);
	}
	return 0;
}

