#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+5,mod=1e9+7;
struct edge{
	int from,to,nxt;
}e[N];
int head[N],ecnt;
void add_edge(int u,int v){
	e[++ecnt]=(edge){u,v,head[u]};
	head[u]=ecnt;
}
bool vis[N];
ll ans=0;
int cnt=0;
int n,k;
void dfs(int x,stack<int> fa){
	bool flg=0;
	stack<int> now;
	now=fa;
	if(cnt==n-1){
		ans=(ans+1)%mod;
		return;
	}
	for(int i=head[e[x].from];i;i=e[i].nxt){
		if(vis[(i+1)/2])continue;
		vis[(i+1)/2]=1;
		now.push(i);
		cnt++;
		dfs(i,now);
		cnt--;
		now.pop();
		vis[(i+1)/2]=0;
		flg=1;
	}
	for(int i=head[e[x].to];i;i=e[i].nxt){
		if(vis[(i+1)/2])continue;
		vis[(i+1)/2]=1;
		now.push(i);
		cnt++;
		dfs(i,now);
		cnt--;
		now.pop();
		vis[(i+1)/2]=0;
		flg=1;
	}
	if(!flg){
		if(!now.empty())now.pop();
		if(now.empty())return;
		dfs(now.top(),now);
	}
}
void solve(){
	memset(head,0,sizeof(head));
	memset(vis,0,sizeof(vis));
	ecnt=ans=0;
	cnt=1;
	cin>>n>>k;
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		add_edge(u,v);
		add_edge(v,u);
	}
	if(c==18){
		cout<<1<<'\n';
		return;
	}
	else if(c>18&&c<=21){
		ans=1;
		for(int i=2;i<=n-2;i++){
			ans=(1ll*ans*i)%mod;
		}
		cout<<(1ll*ans*k)%mod<<"\n";
		return;
	}
	stack<int> now;
	for(int i=1;i<=k;i++){
		memset(vis,0,sizeof(vis));
		int en;
		cin>>en;
		vis[en]=1;
		now.push(en*2);
		dfs(en*2,now);
		now.pop();
		vis[en]=0;
	}
	cout<<ans<<'\n';
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int c,t;
	cin>>c>>t;
	for(int i=1;i<=t;i++)
		solve();
	return 0;
}

