#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PII pair<int,int>
#define fi first
#define se second
#define VI vector<int>
#define PB emplace_back
#define PPB pop_back
#define ALL(x) x.begin(),x.end()
#define endl '\n'
using namespace std;

const int N=100005,MOD=1e9+7,i2=MOD+1>>1;
int n,k,u[N],v[N];
vector<PII> g[N];
VI key;
int pw(int x,int y=MOD-2)
{
	int r=1;
	while(y)
	{
		if(y&1)r=1ll*r*x%MOD;
		x=1ll*x*x%MOD,y>>=1;
	}
	return r;
}

int fac[N];

namespace K8
{
int f[8][N];
void dfs(int id,int x)
{
	if(!f[id][u[x]])
	{
		f[id][u[x]]=x;
		for(auto y:g[u[x]])
			if(y.se!=x)dfs(id,y.se);
	}
	if(!f[id][v[x]])
	{
		f[id][v[x]]=x;
		for(auto y:g[v[x]])
			if(y.se!=x)dfs(id,y.se);
	}
}
void solve()
{
	for(int i=0;i<k;i++)dfs(i,key[i]);
	int ans=0;
	for(int i=1;i<1<<k;i++)
	{
		int cur=1;
		for(int j=1;j<=n;j++)
		{
			VI t;
			for(int l=0;l<k;l++)if((i>>l)&1)t.PB(f[l][j]);
			sort(ALL(t)),t.erase(unique(ALL(t)),t.end());
			if(t.size()>=3){cur=0;break;}
			cur=1ll*cur*fac[g[j].size()-t.size()]%MOD;
		}
		(ans+=(__builtin_parity(i)?cur:MOD-cur))%=MOD;
	}
	cout<<ans<<endl;
	memset(f,0,sizeof(f)); 
}
}

int main()
{
	freopen("traverse.in","r",stdin),freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0);
	
	fac[0]=1;
	for(int i=1;i<N;i++)fac[i]=1ll*fac[i-1]*i%MOD;
	
	int _,TC;cin>>TC>>_;while(_--)
{
	cin>>n>>k;
	for(int i=1;i<n;i++)cin>>u[i]>>v[i],g[u[i]].PB(v[i],i),g[v[i]].PB(u[i],i);
	for(int i=1,x;i<=k;i++)cin>>x,key.PB(x);
	if(TC<=12)K8::solve();
	else if(TC==18)cout<<1<<endl;
	else if(TC>=19&&TC<=21)cout<<1ll*fac[n-3]*(((n-1)*(n-2ll)-(n-k-1)*(n-k-2ll)>>1)%MOD)%MOD<<endl;
	fill(g+1,g+n+1,vector<PII>()),key.clear();
}
	
	return 0;
}
