#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int T,c,st[100005],fr[10];
ll ans,n,k;
int x[100005],y[100005];
bool vis[10];
void dfs(int c){
	if(c==0){
		++ans;
		return;
	}
	bool pd=0;
	for(int i=1;i<n;++i)
	if(!vis[i]&&(x[c]==x[i]||y[c]==x[i]||x[c]==y[i]||y[c]==y[i])){
		vis[i]=1,pd=1,fr[i]=c;
		dfs(i);
		vis[i]=0;
	}
	if(!pd)dfs(fr[c]);
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>c>>T;
	if(c==18){
		while(T--)cout<<"1\n";
		return 0;
	}
	while(T--){
		cin>>n>>k;
		for(int i=1;i<n;++i)
			cin>>x[i]>>y[i];
		for(int i=1;i<=k;++i)cin>>st[i];
		ans=0;
		if(n==2){
			cout<<1<<'\n';
			continue;
		}
		if(c>=19&&c<=21){
			--n;
			ans=1;
			for(int i=1;i<n-1;++i)ans=ans*i%mod;
			cout<<ans*((k*(n-k)%mod+k*(k-1)/2)%mod)%mod<<'\n';
			continue;
		}
		fr[st[1]]=0;vis[st[1]]=1;
		dfs(st[1]);
		cout<<ans<<'\n';
	}
	return 0;
}
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","w",stdin);
//	freopen("traverse.out","r",stdout);
//	freopen("traverse1.in","r",stdin);
//	freopen("traverse2.out","w",stdout);
