#include<bits/stdc++.h>
#define int long long
#define mod 1000000007 
using namespace std;
int T,n,k,C;
int fpow(int a,int b){
	int tans=1;
	while(b){
		if(b&1)tans=tans*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return tans;
}
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&C,&T);
	while(T--){
		if(C==18)printf("1\n");
		else{
			scanf("%lld%lld",&n,&k);
			for(int i=1;i<n;i++){
				int u,v;
				scanf("%lld%lld",&u,&v);
			}
			for(int i=1;i<=k;i++){
				int tmp;
				scanf("%lld",&tmp);
			}
			int ans=1;
			for(int i=1;i<=n-2;i++){
				ans=ans*i%mod;
			}
			printf("%lld\n",(2*k*ans%mod+1)%mod);
		}
	}
	return 0;
}

