#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 3, mod = 1e9 + 7;
int n, q, k, c, T;
struct Edge {
	int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
void add(int u, int v) {
	tot++;
	e[tot] = {v, head[u]};
	head[u] = tot;
}

void solve() {
	cin >> n >> k;
	bool A = 1, B = 1;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		if (u != i || v != i + 1) A = 0;
		if (u != 1 || v != i + 1) B = 0;
	}
	for (int i = 1, u; i <= k; i++) cin >> u;
	if (A) cout << 1 << '\n';
	else {
		n--;
		long long res1 = k;
		for (int i = 1; i < n; i++) {
			res1 = res1 * i % mod;
		}
		long long res2 = 1ll * k * (k - 1) / 2 % mod;
		for (int i = 1; i < n - 1; i++) {
			res2 = res2 * i % mod;
		} 
		cout << (res1 - res2 + mod) % mod << '\n';
	}
}
int main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0); 
	int c, t;
	cin >> c >> t;
	while (t--) solve();
	
	return 0;
}
