#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1000000007;
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int c,t;
	scanf("%lld%lld",&c,&t);
	if(c==18){
		while(t--) printf("1\n");
		return 0;
	}
	while(t--){
		int n,k;
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<n;i++){
			int x,xx;
			scanf("%lld%lld",&x,&xx);
		}
		for(int i=1;i<=k;i++){
			int x;
			scanf("%lld",&x);
		}
		int jc=1;
		for(int i=1;i<=n-1;i++) jc=jc*i%mod;
		printf("%lld\n",jc);
	}
	return 0;
}
