#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+7,mod=1e9+7;
ll a[N],b[N],jc[N];
ll n,m,k,q,t,cnt,tmp,sum,ans,mx=-1e9,x,y,z,w,l,r,mid,f,c;
ll rd(){
	int f=1,x=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
void init(){
	jc[0]=1;
	for(int i=1;i<=1e5;i++)jc[i]=jc[i-1]*i%mod;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	c=rd(),q=rd();init();
	while(q--){
		n=rd(),m=rd();
		for(int i=1;i<n;i++)x=rd(),y=rd();
		for(int i=1;i<=m;i++)x=rd();
		if(c==18)printf("1\n");
		if(c==19)printf("%lld\n",jc[m-1]*m%mod);
	}
	return 0;
}
