#include<iostream>
using namespace std;
const int M=1e9+7;
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int c,T,n,k,t;
	cin>>c>>T;
	while(T--){
		cin>>n>>k;
		for(int i=0;i<n-1;i++){
			cin>>t>>t;
		}
		for(int i=0;i<k;i++){
			cin>>t;
		}
		if(c==18)cout<<"1\n";
		else{
			int ans=1;
			for(int i=n-2;i>0;i--){
				ans*=i;
				ans%=M;
			}
			cout<<ans<<'\n';
		}
	}
	return 0;
}
