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

long long read() {
	long long x = 0, k = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') k = -1, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
	return x * k;
}

const int N = 1e5 + 7;
const long long mod = 1e9 + 7;

int typ;
int n, k;
int u[N], v[N];
vector <int> to[N];
long long ans, fac[N], cnt[N];
int dep[N];

void dfs(int u, int fa, int d) {
	dep[u] = d;
//	cout << u << '\n';
	for (int v : to[u]) {
		if (v != fa) {
			cnt[u] ++;
			dfs(v, u, d + 1);
		}
	}
}

void init() {
	ans = 1;
	for (int i = 1; i <= n; i ++) {
		to[i].clear();
		cnt[i] = 0;
	}
}

void solve(int id) {
	if (typ <= 6) {
		n = read(), k = read();
		init();
		fac[0] = 1;
		for (int i = 1; i <= n; i ++)
			fac[i] = fac[i - 1] * i % mod;
		for (int i = 1; i < n; i ++) {
			u[i] = read(), v[i] = read();
			to[u[i]].push_back(v[i]);
			to[v[i]].push_back(u[i]);
		}
		int x = read();
//		if (id == 1) return ;
//		cout << "yes\n";
		dfs(u[x], 0, 1);
		ans = 1;
//		cout << "yes\n";
		for (int i = 1; i < n; i ++) {
			if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]);
			long long cntu = max(1, (int) to[u[i]].size() - 1);
			long long cntv = max(1, (int) to[v[i]].size() - 1);
			if (i == x) {
				(ans *= fac[cntu] * fac[cntv] % mod) %= mod;
			} else {
				(ans *= fac[cnt[v[i]]]) %= mod;
			}
//			printf("edge : (%d, %d), cnt = (%d, %d)\n", u[i], v[i], cntu, cntv);
		}
		cout << ans << '\n';
	} else if (typ == 18) {
		n = read(), k = read();
		for (int i = 1; i < n; i ++) 
			u[i] = read(), v[i] = read();
		cout << 1 << '\n';
	}
}

signed main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	int t = 1;
	typ = read();
	t = read();
	for (int id = 1; id <= t; id ++)
		solve(id);
	return 0;
}
/*
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
//freopen("edit.in", "r", stdin);
//freopen("edit.out", "w", stdout);
*/

