#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100004;
const ll mod=1e9+7;
ll qpow(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1)res=res*x%mod;
		x=x*x%mod,k>>=1;
	}
	return res;
}
ll fac[maxn];
int c,_;
int n,k;
struct Edge{
	int to,nxt;
}edg[maxn<<1];
struct EDGE{
	int u,v;
}b[maxn];
int hd[maxn],tt;
inline void add(int u,int v){
	edg[++tt]={v,hd[u]};
	hd[u]=tt;
}
bool vis[maxn];
int in[maxn];
ll dfs(int u,int fa){
	int cnts=0;
	ll res=1;
	for(int e=hd[u];e;e=edg[e].nxt){
		int v=edg[e].to;
		if(v==fa) continue;
		res=res*dfs(v,u)%mod;
		cnts++;
		res=res*cnts%mod;
	}
	return res;
}
signed main(){
//	freopen("traverse1.in","r",stdin);
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	fac[0]=1;
	for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;
	cin>>c>>_;
	ll ans=0;
//	ll inv2=qpow(2,mod-2);
	while(_--){
		ll ka=0;
		cin>>n>>k;
		int x,y;
		memset(in,0,sizeof(in));
		memset(hd,0,sizeof(hd));tt=0;
		for(int i=1;i<n;i++){
			cin>>x>>y;
			add(x,y);add(y,x);
			b[i].u=x,b[i].v=y;
			in[x]++,in[y]++;
		}
		int id;
		if(c==18){//lian
			for(int i=1;i<=k;i++){
				cin>>id;
			}
			cout<<1<<'\n';
			continue;
		}
		if(19<=c&&c<=21){
			ans=1ll;
//			int qwq=1;
//			cerr<<inv2<<'\n';
			for(int i=2;i<n;i++){
				ans*=1ll*(n-i)%mod;//*inv2%mod
				ans%=mod;
//				cerr<<ans<<'\n';
			}
			ans=ans*qpow(fac[n-2],mod-2);
			for(int i=1;i<=k;i++){
				cin>>id;
				ka=(ka+ans)%mod;
//				cerr<<ans<<'\n';
			}
			cout<<ka<<'\n';
			continue;
		}
		for(int i=1;i<=k;i++){
			cin>>id;
			memset(vis,0,sizeof(vis));
			if(in[b[i].u]<=1) swap(b[i].u,b[i].v);
			if(in[b[i].v]>1) ans=dfs(b[i].u,b[i].v)*dfs(b[i].v,b[i].u)%mod;//*2%mod
			else ans=dfs(b[i].u,b[i].v);
			cout<<ans<<'\n';
		}
	}
	return 0;
}

