#include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define db double
using namespace std;

const int MAXX=1e5+10;
const int P=1e9+7;
const int M2=500000004;

int C,T;
int n,k;
int sc[MAXX][2];
vector<int> ed[MAXX];
int a[MAXX];
void sol1() { cout<<"1\n";}
void sol2() {
	int tmp=1,tmp2=1;
	for(int i=1;i<n-1;i++) tmp=tmp*i%P;
	for(int i=1;i<n-2;i++) tmp2=tmp2*i%P;
	tmp=tmp*k%P;
	tmp2=tmp2*k%P*(k-1)%P*M2%P;
	tmp=((tmp-tmp2)%P+P)%P;
	cout<<tmp%P<<'\n';
}
int mul[MAXX];
signed main() {
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>C>>T;
	while(T--) {
		cin>>n>>k;
		for(int i=1;i<n;i++) cin>>sc[i][0]>>sc[i][1];
		for(int i=1;i<=k;i++) cin>>a[i];
		if(C==18) sol1();
		else if(19<=C&&C<=21) sol2();
//		else sol();
		
	}

	return 0;
}
