#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
inline long long read(){
	long long x=0,fh=1;
	char ch=getchar(),lst='%';
	while(ch<'0'||ch>'9')lst=ch,ch=getchar();
	if(lst=='-')fh=-1;
	while(ch>='0'&&ch<='9')x=x*10+ch-48,ch=getchar();
	return x*fh;
}
long long ans,fac[100005];
int op,T,n,m,u,v;
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","W",stdout);
	op=read(),T=read();
	fac[0]=1;
	for(int i=1;i<=100004;i++)fac[i]=fac[i-1]*i%mod;
	while(T--){
		n=read(),m=read();
		for(int i=1;i<n;i++){
			u=read(),v=read();
		}
		for(int i=1;i<=m;i++){
			u=read();
			if(op>=19&&op<=21){
				ans+=fac[n-3]*(n-1-i)%mod;
				ans%=mod;
			}
		}
		if(op==18){
			printf("1\n");
		}
		if(op>=19&&op<=21){
			printf("%lld\n",ans);
		}
	}
}
