#include<bits/stdc++.h>
using namespace std;
int c,t,mod=1e9+7;
long long aa(int n1){
	long long ans=1;
	for(int i=1;i<=n1;i++) ans=(ans*n1)%mod;
	return ans;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>t;
	while(t--){
		if(c==18) cout<<1<<endl;
		else{
			int n,k;
			cin>>n>>k;
			for(int i=1;i<n;i++){
				int a,b;
				scanf("%d %d",&a,&b);
			}
			for(int i=0;i<k;i++){
				int a;
				scanf("%d ",&a);
			}
			long long aaa=aa(n-2);
			aaa*=k;
			aaa%=mod;
			cout<<aaa<<endl;
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
} 
