#include<bits/stdc++.h>
using namespace std;

int n, q;
const int mod = 1e9+7;
long long pre[100086];

void fre(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	return;
}

void love(){
	int n, k;
	bool fla = 1, flb = 1;
	scanf("%d%d", &n, &k);
	int u, v;
	for(int i = 1; i < n; i++){
		scanf("%d%d", &u, &v);
		if(u != i || v != i+1) fla = 0;
		if(u != 1 || v != i+1) flb = 0;
	}
	for(int i = 1; i <= k; i++){
		scanf("%d", &u);
	}
	if(fla){
		printf("1\n");
		return;
	}
	if(flb){
	//	printf("%d\n", (1ll * pre[n-1] * k % mod - n * (n - 1) % mod * 500000004 % mod * pre[n - 2] % mod + mod) % mod);
	//	printf("%d\n", (1ll * pre[n-2] * n % mod * k % mod - pre[n-2] * n % mod * (k + 1) % mod * 500000004 % mod + mod) % mod);
		printf("%d\n", (1ll * k * pre[n-1] % mod - 1ll * k * (k - 1) % mod * 500000004 % mod * pre[n-2] % mod + mod) % mod);
	//??? why ?????
	}
}

int main(){
	fre();
	pre[1] = 1;
	for(int i = 2; i <= 100000; i++){
		pre[i] = pre[i-1] * i;
		pre[i] %= mod;
	}
	int T, c;
	scanf("%d%d", &c, &T);
	while(T--) love();
	return 0;
}
