#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
struct Node{
	int v,nxt;
}e[N<<1];
int h[N],idx;
void connec(int u,int v){
	e[++idx]={v,h[u]};
	h[u]=idx;
}
int a[N];
int C,T,n,m;
void solve(){
	cin>>n>>m;
	for(int i=1;i<n;i++){
		int u,v;cin>>u>>v;
		connec(u,v);
		connec(v,u);
	}
	for(int i=1;i<=m;i++) cin>>a[i];
	int res=m;
	for(int i=2;i<=n-2;i++) res=res*i%mod;
	if(C==18) cout<<"1\n";	
	if(19<=C && C<=21) cout<<res%mod<<"\n";
	else cout<<n-1<<"\n";
}
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>C>>T;
	while(T--) solve();
	return 0;
}
