#include<iostream>
using namespace std;
const int mod = 1e9+7;
int main(){
	
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	
	int c,T;
	cin>>c>>T;
	for(int t = 0;t<T;t++){
		int n,k;
		cin>>n>>k;
		for(int i = 0;i<n-1;i++){
			int u,v;
			cin>>u>>v;
		}
		for(int i = 0;i<k;i++){
			int temp;
			cin>>temp;
		}
		if(c == 18){
			cout<<1<<endl;
		}
		else if(19 <= c && c <= 21){
			long long ans = 1;
			if(n == 2){
				cout<<1<<endl;
				continue;
			}
			else{
				long long temp = 1;
				for(int i = 1;i<=n-2;i++){
					temp *= i;
					temp %= mod; 
				}
				ans += k*temp;
				ans %= mod;
				temp = 1;
				for(int i = 1;i<=n-3;i++){
					temp *= i;
					temp %= mod; 
				}
				int temp2 = (temp*k) % mod;
				temp2 *= k-1;
				temp2 %= mod;
				ans += -temp2 + mod;
				ans %= mod;
				cout<<ans<<endl;
			}
		}
	}
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}

