#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+5;
typedef long long ll;
const int mod=1e9+7;
ll fac[N];
int du[N];
void init(){
	fac[0]=1;
	for(int i=1;i<=100001;i++){
		fac[i]=fac[i-1]*i%mod;
	}
	return ;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int c,T;
	cin>>c>>T;
	init();
//	cout<<"qwq";
	while(T--){
		int n,k;
		scanf("%d %d",&n,&k);
		for(int i=1;i<=n-1;i++){
			int u,v;
			scanf("%d %d",&u,&v);
			du[u]++,du[v]++;
		}
		int dup=0,du1=0,du2=0;
		for(int i=1;i<=n;i++){
			if(du[i]==n-1)dup++;
			if(du[i]==1)du1++;
			if(du[i]==2)du2++;
		}
		if(c>=19&&c<=21){
			ll ans=0;
			for(int i=1;i<=k;i++){
				int e;
				scanf("%d",&e);
				ans=(ans+fac[n-2])%mod;
				ans=(ans-i+1)%mod;
			}
			cout<<ans<<"\n";
			continue;
		}
		if(c==18){
			cout<<"1\n";
			continue;
		}
	}
	return 0;
} 
