#include<bits/stdc++.h>
#define ll long long 
#define LL __int128
#define fi first
#define se second
#define int long long
using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n, head[maxn], cnt, fac[maxn], k, a[maxn];

void solve(){
	cin >> n >> k;
	bool flag = 1;
	for(int i = 1; i < n; i++){
		int u, v; cin >> u >> v;
		if(u != 1 || v != i + 1) flag = 0;
	}
	for(int i = 1; i <= k; i++) cin >> a[i];
	fac[0] = 1; for(int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;
	if(flag){
		int ans = 0;
		for(int i = 1; i <= k; i++) ans = (ans + fac[n - 1] % mod) % mod;
		cout << ans << '\n';	
		return;
	}
}

signed main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);

	int c, t; cin >> c >> t; while(t--) solve();

	return 0;
}