#include<iostream>
#define int long long 
using namespace std;

//struct edge{
//	int to,nxt;
//};

const int mod=1e9+7;
const int inv2=5e8+4;
const int MAXN=1e5+5;
int c,t,n,k;

int fac(int m){
	int ans=1;
	for(int i=1;i<=m;i++){
		ans*=i;
		ans%=mod;
	}
	return ans;
} 

signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>c>>t;
	while(t--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
		}
		for(int i=1;i<=k;i++){
			int num;
			cin>>num;
		}
		if(n==2) cout<<1<<'\n';
		else if(c==18) cout<<1<<'\n';
		else if(19<=c && c<=21)	cout<<(k*fac(n-2)%mod-k*(k-1)/2%mod*fac(n-3)%mod+mod)%mod<<'\n';
	}
	return 0;
}

//12:19 过A&&B,244pts 
//12:32 244pts应该够了,开始乱搞了吧 
