#include<bits/stdc++.h>
#define gt getchar()
#define pt putchar
#define ll long long
using namespace std;
ll getin(){
	ll ans=0,f=1;
	char c=gt;
	while(!isdigit(c)){
		if(c=='-') f=-1;
		c=gt;
	}
	while(isdigit(c)){
		ans=(ans<<1)+(ans<<3)+(c^48);
		c=gt;
	}
	return ans*f;
}
void puto(ll x){
	if(x<0){
		pt('-');
		x=-x;
	}
	if(x>=10) puto(x/10);
	pt(x%10+'0');
}
const ll N=2e5+5,mo=1e9+7;
ll h[N],cnt;
struct node{
	ll to,next;
}e[N];
void add(ll u,ll v){
	cnt++;
	e[cnt].next=h[u];
	e[cnt].to=v;
	h[u]=cnt;
}
ll c,T;
ll a[N];
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>T;
	while(T--){
		cnt=0;
		memset(e,0,sizeof(e));
		memset(h,0,sizeof(h));
		memset(a,0,sizeof(a));
		ll n,k;
		n=getin(),k=getin();
		for(ll i=1;i<n;i++){
			ll u,v;
			u=getin(),v=getin();
			add(u,v);add(v,u);
		}
		for(ll i=1;i<=k;i++) a[i]=getin();
		if(c==18) pt('1');
		else if(c==19||c==20){
			ll ans=1;
			for(int i=1;i<n-1;i++) ans=(ans*i)%mo;
			puto((ans*k)%mo);
		}
		pt('\n');
	}





	return 0;
}


