#include <bits/stdc++.h>

const int mod = 1e9 + 7;

void solve1() {
  int n, k; scanf("%d %d", &n, &k);
  std::vector<std::vector<int>> e(n);
  for (int i = 1, u, v; i < n; i++) {
    scanf("%d %d", &u, &v), --u, --v;
    e[u].push_back(v), e[v].push_back(u);
  }
  std::vector<int> c(k);
  for (int &x : c) scanf("%d", &x);
  std::vector<int> fac(n + 1);
  for (int i = fac[0] = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  int ans = 1;
  for (int i = 0; i < n; i++)
    ans = 1ll * ans * fac[e[i].size() - 1] % mod;
  printf("%d\n", ans);
}

void solve2() {
  int n, k; scanf("%d %d", &n, &k);
  std::vector<std::vector<int>> e(n);
  for (int i = 1, u, v; i < n; i++) {
    scanf("%d %d", &u, &v), --u, --v;
    e[u].push_back(v), e[v].push_back(u);
  }
  std::vector<int> c(k);
  for (int &x : c) scanf("%d", &x);
  if (n <= 3) return printf("1\n"), void();
  std::vector<int> fac(n + 1);
  for (int i = fac[0] = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  int ans = 1ll * k * fac[n - 2] % mod;
  (ans += mod - 1ll * k * (k - 1) / 2 % mod * fac[n - 3] % mod) %= mod;
  printf("%d\n", ans);
}

int main() {
  freopen("traverse.in", "r", stdin);
  freopen("traverse.out", "w", stdout);
  int c, T; scanf("%d %d", &c, &T);
  if (c <= 6) while (T--) solve1();
  if (c == 18) while (T--) printf("1\n");
  if (c >= 19 && c <= 21) while (T--) solve2();
}
// expected point : 40 
