#include<bits/stdc++.h>
#define LL long long
#define mod 1000000007
#define N 100005
using namespace std;
LL fc[N];
int c,_,n,k,cnt[N];
vector <array <int,2> > T[N];
int PP[N];
LL ret;
void DFS(int u,int f){
	for(auto v:T[u])
		if(v[1]^f){
			DFS(v[1],u);
		}
	ret=ret*fc[cnt[u]]%mod;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	fc[0]=1;for(int i(1);i<N;++i) fc[i]=fc[i-1]*i%mod;
	scanf("%d%d",&c,&_);
	while(_--){
		scanf("%d%d",&n,&k);
		for(int i(1);i<=n;++i){
			cnt[i]=0;
			T[i].clear();
		}
		for(int i(1);i<n;++i){
			int u,v;scanf("%d%d",&u,&v);
			T[u].push_back({i,v});
			T[v].push_back({i,u});
		}
		if(k==1){
			int xx;scanf("%d",&xx);xx=PP[xx];
			ret=1;DFS(xx,0);
			printf("%lld\n",ret);
		}
		else if(c==18){
			for(int i(1);i<=k;++i){
				int xx;scanf("%d",&xx);
			}
			puts("1");
		}
		else if(19<=c&&c<=21){
			for(int i(1);i<=k;++i){
				int xx;scanf("%d",&xx);
			}
			printf("%lld\n",k*fc[n-1]%mod);
		}
	}
	return 0;
}
