#include<bits/stdc++.h>
using namespace std;
long long c,t,n,k,x,y,i,dp[100005],jc[100005]={1};
vector<long long>v[1000005];
pair<long long,long long>e[100005];
long long ksm(long long a,long long b){
	long long r=1;
	while(b){
		if(b&1)r=r*a%1000000007;
		a=a*a%1000000007;
		b>>=1;
	}
	return r;
}
long long dfs(long long x,long long y){
	dp[x]=1;
	long long c,i;
	for(c=i=0;i<v[x].size();i++){
		if(v[x][i]!=y){
			c++;
			dp[x]=dp[x]*dfs(v[x][i],x)%1000000007;
		}
	}
	return dp[x]=dp[x]*jc[c]%1000000007;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&t);
	for(i=1;i<=100000;i++)jc[i]=jc[i-1]*i%1000000007;
	while(t--){
		scanf("%lld%lld",&n,&k);
		for(i=1;i<n;i++){
			scanf("%lld%lld",&x,&y);
			e[i]=make_pair(x,y);
			v[x].push_back(y);
			v[y].push_back(x);
		}
		if(c==18){
			while(k--)scanf("%lld",&x);
			printf("1\n");
			continue;
		}
		if(c==19||c==20||c==21){
			if(k==1){
				scanf("%lld",&x);
				if(x==1)printf("%lld\n",dfs(e[x].first,e[x].second)*dfs(e[x].second,e[x].first)%1000000007);
				else printf("%lld\n",ksm(2,n-2)-1);
			}else{
				while(k--)scanf("%lld",&x);
				printf("%lld\n",ksm(2,n-2)-1);
			}
			continue;
		}
		while(k--){
			scanf("%lld",&x);
			printf("%lld\n",dfs(e[x].first,e[x].second)*dfs(e[x].second,e[x].first)%1000000007);
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
