#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+5, MOD=1e9+7;

int c,T;
int n,k; 
int u[N],v[N];

int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	
	scanf("%d%d",&c,&T);
	while(T--){
		scanf("%d%d",&n,&k);
		bool lian=1,jh=1;
		for(int i=1;i<n;i++){
			scanf("%d%d",&u[i],&v[i]);
			if(u[i]!=i || v[i]!=i+1) lian=0;
			if(u[i]!=1 || v[i]!=i+1) jh=0;
		}
		int e;
		for(int i=1;i<=k;i++){
			scanf("%d",&e);
		}
		
		if(lian){
			printf("1\n");
			continue;
		}
		else if(jh){
			LL ans=1;
			for(int i=1;i<=n;i++) 
				ans=ans*i%MOD;
			printf("%lld\n",ans*k%MOD);
		}
		else printf("0\n");
	}
	return 0;
}


