#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=5e5+5,mod=1e9+7;
int T,n,m;
struct edge { int u,v; } e[N];
int ky[N];
LL qpow(LL a,LL b)
{
	LL ret=1;
	while(b) { if(b&1) ret=ret*a%mod; b>>=1; a=a*a%mod; }
	return ret;
}
vector<int> g[N];
LL f[N];
void AddEdge(int u,int v) { g[u].push_back(v); g[v].push_back(u); }
void DP1(int u,int fa)
{
	f[u]=1; LL fac=1,t=1;
	for(int v: g[u])
	{
		if(v==fa) continue;
		DP1(v,u);
		f[u]=f[u]*f[v]%mod;
		fac=fac*t%mod; t++;
	}
	if(u>n) f[u]=f[u]*fac%mod;
}
void Solve1()
{
	for(int i=1;i<n;i++)
		AddEdge(i,e[i].u+n),AddEdge(i,e[i].v+n);
	DP1(ky[1],0);
	printf("%lld\n",f[ky[1]]);
	for(int i=1;i<=2*n;i++) g[i].clear();
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int ID; scanf("%d",&ID);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<n;i++) scanf("%d%d",&e[i].u,&e[i].v);
		for(int i=1;i<=m;i++) scanf("%d",&ky[i]);
		if(ID<=6)  Solve1();
		if(ID==18) puts("1");
		if(19<=ID&&ID<=21)
		{
			LL ans=1,sum=1;
			for(int i=1;i<=n-2;i++) ans=ans*i%mod;
			for(int i=1;i<=n-3;i++) sum=sum*i%mod;
			printf("%lld\n",(ans*m%mod-1ll*m*(m-1)/2%mod*sum%mod+mod)%mod);
		}
	}
	return 0;
}
/*
1 1
4 1
1 2
2 3
2 4
1
*/
