#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dec(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long LL;
const int M=1e9+7,N=1e5+5;
int c,T,n,k,P[N],e[N];
inline int Rd(){
	int s=0,w=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
	return s*w;
}
LL qp(LL a,LL b){
	LL res=1;
	while (b){
		if(b&1) res=res*a%M;
		a=a*a%M;
		b>>=1;
	}
	return res;
}
void SubA(){
	puts("1");return ;
}
void SubB(){
	printf("%lld\n",1ll*k*P[n-2]%M);
	return ;
}
void Sol(){
	n=Rd();k=Rd();
//	printf("%d %d\n",n,k);
	int A=1,B=1;
	rep(i,1,n-1){
		int u=Rd(),v=Rd();
		if(u!=1) B=0;if(u!=i||v!=i+1) A=0;
	}
	rep(i,1,k) e[i]=Rd();
	if(A) return SubA();
	return SubB();
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
//printf("%lld\n",qp(99689,M-2));
	P[0]=1;rep(i,1,N-1) P[i]=1ll*P[i-1]*i%M;
	c=Rd();T=Rd();
	while (T--) Sol();
	return 0;
}/*
100000 99689
695424595

100000 99690
944365758
*/
