#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e5+5,mod=1e9+7;
int n,e[N],f[505][505];
bool vis[505][505];
ull dfs(int x,int y){
	vis[x][y]=vis[y][x]=1;
	ull tot=0;
	for (int i=1;i<=n;i++){
		if (f[x][i]&&!vis[x][i])  tot=(tot+dfs(x,i))%mod;
		if (f[y][i]&&!vis[y][i])  tot=(tot+dfs(y,i))%mod;
	}
	vis[x][y]=vis[y][x]=0;
	if (tot==0)  return 1;
	else  return tot;
}
void work(){
	memset(f,0,sizeof(f));
	memset(vis,0,sizeof(vis));
	int k;
	bool spea=1,speb=1;
	scanf("%d %d",&n,&k);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		if (!(x==i&&y==i+1))  spea=0;
		if (!(x==1&&y==i+1))  speb=0;
		if (x<505&&y<505)  f[x][y]=f[y][x]=i;
	}
	for (int i=1;i<=k;i++)  scanf("%d",&e[i]);
	if (spea){
		ull ans=0;
		bool u=0;
		for (int i=1;i<=k;i++){
			if (e[i]==1||e[i]==n-1){
				if (!u)  u=1,ans=(ans+1)%mod;
			}
			else  ans=(ans+2)%mod;
		}
		printf("%llu\n",ans);
		return;
	}
	if (speb){
		ull jc1,jc2=1,ans=0;//jc1=(n-2)!,jc2=(n-3)!
		for (int i=2;i<=n-3;i++)  jc2=jc2*i%mod;
		jc1=jc2*(n-2)%mod;
		for (int i=1;i<=k;i++)  ans=(ans+jc1-jc2*(i-1)+mod)%mod;
		printf("%llu\n",ans);
		return;
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (f[i][j]==e[1]){
				printf("%llu\n",dfs(i,j));
				return;
			}
	return;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int id,t;
	scanf("%d %d",&id,&t);
	while (t--)  work();
	return 0;
}
//freopen("traverse.in","r",stdin);
//freopen("traverse.out","w",stdout);
//froepen("traverse.in","r",stdin);
//froepen("traverse.out","w",stdout);
//freopen("traverse.in","w",stdin);
//freopen("traverse.out","r",stdout);
//freopen("traverse.in",r,stdin);
//freopen("traverse.out",w,stdout);
/*
国王的铠甲：护盾值+3，将所有目标生命转化为护盾值（不会低于1），每场战斗后使目标生命-1，护盾值+1
国王的新枪：目标生命为1时，我方所有单位攻击速度+50
国王的延伸：目标生命为1时，我方所有单位每2秒恢复1点技力和3%的生命值
诸王的冠冕：目标生命为1时，我方所有单位攻击力+50%，集齐三件及以上国王收藏品时变为+150%
*/
