#include<iostream>
#include<cstdio>
using namespace std;
long long c,t;
long long n,k;
const long long mod=1e9+7;
long long fac[100005];
long long ans;
signed main(){
//	freopen("traverse9.in","r",stdin);
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&t);
	if(c==18){
		while(t--){
			scanf("%lld%lld",&n,&k);
			for(int i=1;i<n;i++){
				long long x,y;
				scanf("%lld%lld",&x,&y);
			}
			for(int i=1;i<=k;i++){
				long long x;
				scanf("%lld",&x);
			}
			printf("1\n");
		}
		return 0;
	}else if(c>=19&&c<=21){
		ans=0;
		fac[0]=1ll;
		for(long long i=1ll;i<=100000ll;i++)fac[i]=fac[i-1]*i%mod;
		while(t--){
			scanf("%lld%lld",&n,&k);
			for(int i=1;i<n;i++){
				int x,y;
				scanf("%d%d",&x,&y);
			}
			for(int i=1;i<=k;i++){
				int x;
				scanf("%d",&x);
			}
			if(n>3&&k>1)ans=(fac[n-3]*(k*(k-1)/2%mod))%mod;
			if(n>3)ans=(ans+(n-1-k)*k%mod*fac[n-3]%mod)%mod;
			printf("%lld\n",ans);
		}
	}
	return 0;
}
