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

#define int unsigned long long
const int N = 200000 + 10;
const int mod = 1e9 + 7;
int n, k;
int a[N], b[N], f[N], t[N];
void solve(){
	bool flg1 = 1, flg2 = 1;
	cin >> n >> k;
	for(int i = 1 ; i < n ; i ++){
		cin >> a[i] >> b[i];
		if(a[i] != i || b[i] != i + 1){
			flg1 = 0;
//			cout << a[i] << ' ' << b[i] << ' ' << i << '\n';
		}
		if(a[i] != 1 || b[i] != i + 1) flg2 = 0;
	}
	for(int i = 1 ; i <= k ; i ++)
		cin >> f[i];
	if(flg1){
//		cout << "!\n";
		cout << 1 << '\n';
		return;
	}
	if(flg2){
//		cout << "got1\n";
	t[0] = 1;
		for(int i = 1 ; i <= n ; i ++)
			t[i] = t[i - 1] * i % mod;
		cout << (k * t[n - 2] % mod + mod - ((k * (k - 1) / 2) % mod * t[n - 3]) % mod) % mod << '\n';
	}
}

signed main(){
	 freopen("traverse.in", "r", stdin);
	 freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(false);
	int c, t;
	cin >> c >> t;
	while(t --)
		solve();
}
