#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
namespace io{
const int BFSZ=1<<22;
char rbuffer[BFSZ+5];
int rpt,rsz;
char gc()
{
	if(rpt>=rsz)
	{
		rsz=fread(rbuffer,sizeof(char),BFSZ,stdin);
		if(rsz==0)
		{
			return EOF;
		}
		rpt=0;
	}
	return rbuffer[rpt++];
}
template<typename T>
void read(T &x)
{
	x=0;
	int f=1;
	char c=gc();
	while(c<'0'||c>'9')
	{
		if(c=='-')
		{
			f=-1;
		}
		c=gc();
	}
	while(c>='0'&&c<='9')
	{
		x=(x<<1)+(x<<3)+int(c-'0');
		c=gc();
	}
	x*=f;
}
template<typename T>
void write(T x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>=10)
	{
		write(x/10);
	}
	putchar('0'+int(x%10));
}}
namespace cyrxdzj{
//#define debug
//#define check_memory
#ifdef check_memory
bool check_memory_st;
#endif
const int MAXN=2e5;
const long long MOD=1e9+7;
long long qpow(long long a,long long b);
const long long INV2=qpow(2,MOD-2);
int tcid,tccnt;
long long fac[MAXN+5],inv[MAXN+5];
int n,keycnt;
int key[MAXN+5];
struct IEdge
{
	int u,v;
}iedge[MAXN+5];
int deg[MAXN+5];
bool flagA,flagB;
#ifdef check_memory
bool check_memory_ed;
#endif
long long qpow(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1)
		{
			res=res*a%MOD;
		}
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}
long long C(int tot,int choose)
{
	if((tot<choose)||(choose<0))
	{
		return 0;
	}
	return fac[tot]*inv[tot-choose]%MOD*inv[choose]%MOD;
}
int main()
{
	#ifdef check_memory
	printf("check_memory %lf\n",((double)((&check_memory_st)-(&check_memory_ed)))/1024/1024);
	#endif
	freopen("traverse.in","r",stdin);
	#ifndef debug
	freopen("traverse.out","w",stdout);
	#endif
	fac[0]=1;
	for(int i=1;i<=MAXN;i++)
	{
		fac[i]=fac[i-1]*i%MOD;
	}
	inv[MAXN]=qpow(fac[MAXN],MOD-2);
	for(int i=MAXN-1;i>=0;i--)
	{
		inv[i]=inv[i+1]*(i+1)%MOD;
	}
	io::read(tcid);
	io::read(tccnt);
	while(tccnt--)
	{
		flagA=flagB=true;
		io::read(n);
		io::read(keycnt);
		for(int i=1;i<=n;i++)
		{
			deg[i]=0;
		}
		for(int i=1;i<n;i++)
		{
			io::read(iedge[i].u);
			io::read(iedge[i].v);
			flagA&=(iedge[i].u==i);
			flagA&=(iedge[i].v==i+1);
			flagB&=(iedge[i].u==1);
			flagB&=(iedge[i].v==i+1);
			deg[iedge[i].u]++;
			deg[iedge[i].v]++;
		}
		for(int i=1;i<=keycnt;i++)
		{
			io::read(key[i]);
		}
		long long ans=1;
		if(flagA)
		{
			ans=1;
		}
		else if(keycnt==1)
		{
			for(int i=1;i<=n;i++)
			{
				ans=ans*fac[deg[i]-1]%MOD;
			}
		}
		else if(flagB)
		{
			ans=((keycnt*fac[n-2]%MOD)-(C(keycnt,2)*fac[n-3]%MOD)+2*MOD)%MOD;
		}
		else
		{
			for(int i=1;i<=n;i++)
			{
				ans=ans*fac[deg[i]-1]%MOD;
			}
		}
		io::write(ans);
		putchar('\n');
	}
	return 0;
}}
int main()
{
	return cyrxdzj::main();
}
