#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return (f?-x:x);
}
const int M=1e5+5,mod=1e9+7;
const ull bite=13331;
int id,T,n,k,x[M],y[M],f[M],sub,bj[M],a[M];
ll fac[M],ans;
vector<int> g[M],p[M];
map<int,int> po;
void dfs(int x,int id)
{
	if(id==n)
	{
		ull he1=0,pos1=1,he2=0,pos2=1;
		for(int i=1;i<=n;++i,pos1=pos1*bite)
			he1+=1ull*f[i]*pos1;
		for(int i=1;i<=n;++i,pos2=pos2*bite)
			he2+=1ull*f[i]*pos2;
		if(po[he1]||po[he2])return;
		po[he1]=po[he2]=1;
		ans=(ans+1)%mod;
	}
	for(int v:p[x])
		if(!bj[v])
		{
			f[id+1]=v;
			bj[v]=1;
			dfs(v,id+1);
			bj[v]=0;
		}
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	fac[0]=1;
	for(int i=1;i<=1e5;++i)fac[i]=fac[i-1]*i%mod;
	id=read();T=read();
	while(T--)
	{
		n=read();k=read();sub=0;
		for(int i=1;i<=n;++i)g[i].clear(),p[i].clear(),bj[i]=f[i]=0;
		for(int i=1;i<n;++i)
		{
			x[i]=read();y[i]=read();
			g[x[i]].push_back(i);
			g[y[i]].push_back(i);
		}
		if(n<=5)
		{
			po.clear();
			ans=0;
			for(int i=1;i<n;++i)
			{
				for(int v:g[x[i]])
					if(v!=i)p[i].push_back(v);
				for(int v:g[y[i]])
					if(v!=i)p[i].push_back(v);
			}
			for(int i=1;i<=k;++i)
			{
				memset(bj,0,sizeof(bj));
				f[1]=a[i]=read();
				bj[a[i]]=1;
				dfs(a[i],2);
			}
			printf("%lld\n",ans);
			continue;
		}
		for(int i=1;i<=k;++i)
			a[i]=read();
		printf("%lld\n",fac[k]);
	}
	return 0;
}
