#include<bits/stdc++.h>
#define ri register int
using namespace std;
const long long mo=1e9+7;
int c,T,n,k,cnt,pd[200005],bs[2000005],hed[2000005],net[2000005],to[2000005],C,H[200005],N[200005],To[200005],a[200005];
long long ans;
void add(ri x,ri y)
{
	net[++cnt]=hed[x];
	to[cnt]=y;
	hed[x]=cnt;
}
void A(ri x,ri y)
{
	N[++C]=H[x];
	To[C]=y;
	H[x]=C;
}
void dfs(ri x,ri y)
{
	if(y==n-1)
	{
		ans++;
		return;
	}
	for(ri i=hed[x];i;i=net[i])
	{
		if(pd[to[i]]==0)
		{
			pd[to[i]]=1;
			dfs(to[i],y+1);
			pd[to[i]]=0;
		}
	}
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&c,&T);
	while(T--)
	{
		ans=0;
		C=cnt=0;
		scanf("%d%d",&n,&k);
		for(ri i=1;i<n;++i)
		{
			ri x,y;
			scanf("%d%d",&x,&y);
			A(x,i);
			A(y,i);
			bs[i]=0;
		}
		if(c==18)
		{
			ans=0;
			while(k--)
			{
				ri x;
				scanf("%d",&x);
			}
			printf("1\n");
			continue;
		}
		if(19<=c&&c<=21)
		{
			ans=0;
			for(ri i=1;i<=k;++i)
			{
				ri x;
				scanf("%d",&x);
			}
			printf("%lld\n",k);
			continue;
		}
		for(ri i=1;i<n;++i)
		{
			ri x,y;
			scanf("%d%d",&x,&y);
			A(x,i);
			A(y,i);
			bs[i]=0;
		}
		for(ri i=1;i<=n;++i)
		{
			ri ji=0;
			for(ri j=H[i];j;j=N[j])
			{
				ji++;
				a[ji]=To[j];
			}
			for(ri j=1;j<ji;++j)
			{
				for(ri w=j+1;w<=ji;++w)
				{
					bs[a[j]]++;
					bs[a[w]]++;
					add(a[j],a[w]);
					add(a[w],a[j]);
				}
			}
		}
		ans=0;
		while(k--)
		{
			ri x;
			scanf("%d",&x);
			memset(pd,0,sizeof(pd));
			pd[x]=1;
			dfs(x,1);
		}
		ans%=mo;
		printf("%lld\n",ans);
	}
	return 0;
} 
