#include <bits/stdc++.h>
using namespace std;
inline int Get() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const long long mod = 1e9 + 7;
const long long Mod = 998244353;
int n, m;
long long f[2005][15];
struct road {
  int to, next;
} s[2005 << 1];
int h[2005], cnt;
void add(int i, int j) {
  s[++cnt] = (road){j, h[i]};
  h[i] = cnt;
}
vector<int> e[2005];
long long fac[2005], ifac[2005];
long long ksm(long long t, long long x) {
  long long ans = 1;
  for (; x; x >>= 1, t = t * t % mod)
    if (x & 1) ans = ans * t % mod;
  return ans;
}
long long Hash[2005];
long long Hash2[2005];
const long long p = 666233, p2 = 134341;
unsigned long long p3 = 233333;
int size[2005];
bool cmp(int a, int b) {
  if (size[a] != size[b]) return size[a] < size[b];
  if (Hash[a] != Hash[b]) return Hash[a] < Hash[b];
  return Hash2[a] < Hash2[b];
}
long long t[2005];
vector<int> sn[2005];
int dep[2005], mxdep[2005];
void dfs1(int v, int fr) {
  size[v] = 1;
  mxdep[v] = dep[v];
  for (int i = 0; i < e[v].size(); i++) {
    int to = e[v][i];
    if (to == fr) continue;
    sn[v].push_back(to);
    dep[to] = dep[v] + 1;
    dfs1(to, v);
    mxdep[v] = max(mxdep[v], mxdep[to]);
    size[v] += size[to];
  }
  sort(sn[v].begin(), sn[v].end(), cmp);
  t[v] = 1;
  for (int i = 0; i < sn[v].size(); i++) {
    int j = i;
    while (j + 1 < sn[v].size() && size[sn[v][i]] == size[sn[v][j + 1]] &&
           Hash[sn[v][i]] == Hash[sn[v][j + 1]] &&
           Hash2[sn[v][j]] == Hash2[sn[v][j + 1]])
      j++;
    t[v] = t[v] * ifac[j - i + 1] % mod;
    i = j;
  }
  Hash2[v] = Hash[v] = '(';
  long long now = 1, now2 = 1;
  for (int i = 0; i < sn[v].size(); i++) {
    Hash[v] = ((Hash[v] * now % mod) ^ Hash[sn[v][i]]) % mod;
    Hash2[v] = ((Hash2[v] * now2 % Mod) ^ Hash2[sn[v][i]]) % Mod;
    now = now * p % mod;
    now2 = now2 * p2 % Mod;
  }
  Hash[v] = (Hash[v] * p + ')') % mod;
  Hash2[v] = (Hash2[v] * p + ')') % Mod;
}
int st[2005], top;
int cal(int v, int u) {
  if (!top) return 0;
  static long long ans[1 << 13], g[1 << 13];
  int num = sn[u].size();
  for (int i = 0; i < 1 << num; i++) ans[i] = 0;
  ans[0] = 1;
  for (int i = 1; i <= top; i++) {
    memcpy(g, ans, sizeof(g));
    int now = st[i];
    for (int j = 0; j < num; j++) {
      if (!f[now][sn[u][j]]) continue;
      int t = 1 << j;
      for (int sta = 0; sta < 1 << num; sta++) {
        if (!(sta & t))
          g[sta | t] = (g[sta | t] + ans[sta] * f[now][sn[u][j]]) % mod;
      }
    }
    memcpy(ans, g, sizeof(g));
  }
  return ans[(1 << num) - 1] * t[u] % mod;
}
long long ans;
int rt;
void solve(int v, int fr) {
  for (int i = 1; i <= m; i++)
    if (sn[i].size() == 0) f[v][i] = 1;
  for (int i = h[v]; i; i = s[i].next) {
    int to = s[i].to;
    if (to == fr) continue;
    solve(to, v);
  }
  top = 0;
  for (int i = h[v]; i; i = s[i].next) {
    if (s[i].to == fr) continue;
    st[++top] = s[i].to;
  }
  for (int i = 1; i <= m; i++) {
    if (sn[i].size()) {
      f[v][i] = cal(v, i);
    }
  }
  (ans += f[v][rt]) %= mod;
}
map<pair<int, int>, int> vis;
int work() {
  for (int i = 1; i <= m; i++) sn[i].clear();
  for (int i = 1; i <= m; i++) Hash[i] = Hash2[i] = 0;
  dep[rt] = 1;
  dfs1(rt, 0);
  if (vis.find(make_pair(Hash[rt], Hash2[rt])) != vis.end()) return 0;
  vis[make_pair(Hash[rt], Hash2[rt])] = 1;
  memset(f, 0, sizeof(f));
  ans = 0;
  solve(1, 0);
  return ans;
}
int main() {
  n = Get();
  int a, b;
  for (int i = 1; i < n; i++) {
    a = Get(), b = Get();
    add(a, b), add(b, a);
  }
  m = Get();
  fac[0] = 1;
  for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % mod;
  ifac[m] = ksm(fac[m], mod - 2);
  for (int i = m - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;
  for (int i = 1; i < m; i++) {
    a = Get(), b = Get();
    e[a].push_back(b);
    e[b].push_back(a);
  }
  long long tot = 0;
  for (rt = m; rt >= 1; rt--) (tot += work()) %= mod;
  cout << tot << "\n";
  return 0;
}
