#include <bits/stdc++.h>
using namespace std;
const int N = 2010, b1 = 123, b2 = 2333, P = 1e9 + 7;
int u, v, n, m, a[1 << 15], b[1 << 15], C[N][20], ans, fac[20];
set<unsigned int> mp;
int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1) ans = 1ll * ans * a % P;
  return ans;
}
struct Graph {
  int hed[N], to[N << 1], nex[N << 1], ed;
  Graph() {
    memset(hed, -1, sizeof(hed));
    ed = -1;
  }
  inline void add(int u, int v) {
    to[++ed] = v;
    nex[ed] = hed[u];
    hed[u] = ed;
  }
} G, H;
namespace T {
int to[20], siz[20], q[20], vis[20], tot, S;
unsigned int f[20], fxk;
inline void cls() { memset(to, 0, sizeof(to)); }
inline void add(int u, int v) { to[u] |= 1 << v; }
void dfs(int u) {
  siz[u] = 1;
  f[u] = 0;
  for (int i = to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
    dfs(v), siz[u] += siz[v];
  tot = 0;
  for (int i = to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
    q[++tot] = v;
  sort(q + 1, q + tot + 1, [&](int x, int y) { return f[x] < f[y]; });
  f[u] = siz[u];
  for (int i = (1); i <= (tot); ++i) f[u] = f[u] * b1 + f[q[i]];
  f[u] *= b2 * siz[u];
  S = 0;
  for (int i = (1); i <= (tot); ++i)
    (f[q[i]] > 1 && f[q[i]] == f[q[i - 1]]) ? (vis[q[i]] = S, S |= 1 << q[i])
                                            : (vis[q[i]] = 0, S = 1 << q[i]);
}
}  // namespace T
void dfs(int u, int fa) {
  for (int i = H.hed[u], v = H.to[i]; ~i; i = H.nex[i], v = H.to[i])
    if (v ^ fa) T::add(u, v), dfs(v, u);
}
int bo[20], g[1 << 15], lf[20], dp[N][20];
void DFS(int u, int fa) {
  int tot = 0;
  for (int i = G.hed[u], v = G.to[i]; ~i; i = G.nex[i], v = G.to[i])
    if (v ^ fa) DFS(v, u), ++tot;
  for (int i = (0); i <= (m - 1); ++i) {
    if (bo[i]) continue;
    if (tot < a[T::to[i]]) {
      dp[u][i] = 0;
      continue;
    }
    for (int j = T::to[i]; j; j = (j - 1) & T::to[i]) g[j] = 0;
    g[0] = 1;
    for (int j = G.hed[u], v = G.to[j]; ~j; j = G.nex[j], v = G.to[j])
      if (v ^ fa)
        for (int k = T::to[i]; k; k = (k - 1) & T::to[i])
          for (int p = k; p; p -= p & (-p))
            if ((T::vis[b[p & (-p)]] & k) == T::vis[b[p & (-p)]])
              g[k] = (g[k] + 1ll * dp[v][b[p & (-p)]] * g[k ^ (p & (-p))]) % P;
    dp[u][i] = 1ll * g[T::to[i]] * C[tot - a[T::to[i]]][lf[i]] % P;
  }
}
int times(int rt) {
  for (int i = (0); i <= (m - 1); ++i) bo[i] = T::siz[i] == 1, lf[i] = 0;
  for (int u = (0); u <= (m - 1); ++u)
    for (int i = T::to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
      (T::siz[v] == 1) && (T::to[u] ^= 1 << v, ++lf[u]);
  DFS(1, 0);
  int ans = 0;
  for (int i = (1); i <= (n); ++i) ans = (ans + dp[i][rt]) % P;
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n - 1); ++i)
    scanf("%d%d", &u, &v), G.add(u, v), G.add(v, u);
  scanf("%d", &m);
  for (int i = (1); i <= (m - 1); ++i)
    scanf("%d%d", &u, &v), --u, --v, H.add(u, v), H.add(v, u);
  if (m == 1) {
    printf("%d\n", n % P);
    return 0;
  }
  a[1] = 1;
  for (int i = (2); i <= ((1 << m) - 1); ++i)
    a[i] = a[i >> 1] + (i & 1), b[i] = b[i >> 1] + 1;
  fac[0] = 1;
  for (int i = (1); i <= (15); ++i) fac[i] = 1ll * fac[i - 1] * i % P;
  C[0][0] = 1;
  for (int i = (1); i <= (15); ++i) {
    C[i][0] = 1;
    for (int j = (1); j <= (i); ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
  }
  for (int i = (16); i <= (n); ++i) {
    C[i][0] = 1;
    for (int j = (1); j <= (15); ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
  }
  for (int i = (0); i <= (m - 1); ++i) {
    T::cls();
    dfs(i, -1);
    T::dfs(i);
    T::fxk = T::f[i];
    if (mp.count(T::fxk)) continue;
    mp.insert(T::fxk);
    ans = (ans + times(i)) % P;
  }
  printf("%d\n", ans);
  return 0;
}
