#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long Inf = 1e10;
const int P = 1e9 + 7;
const int N = 1005;
const int M = 14;
inline long long IN() {
  long long x = 0;
  int ch = 0, f = 0;
  for (ch = getchar(); ch != -1 && (ch < 48 || ch > 57); ch = getchar())
    f = (ch == '-');
  for (; ch >= 48 && ch <= 57; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return f ? (-x) : x;
}
template <typename T>
inline int chkmin(T &a, const T &b) {
  if (b < a) return a = b, 1;
  return 0;
}
template <typename T>
inline int chkmax(T &a, const T &b) {
  if (b > a) return a = b, 1;
  return 0;
}
void renew(int &x, const int &y) {
  x += y;
  if (x >= P) x -= P;
  if (x < 0) x += P;
}
int Pow(int x, int y, int p) {
  int a = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) a = (long long)a * x % p;
  return a;
}
int dp[N][M * M], tot;
map<vector<int>, int> mps;
int sz_info[N], chd_info[N];
vector<int> chd_hash[M * M];
vector<int> e[N], xe[N];
pair<int, int> dfs(int x, int y) {
  vector<int> s;
  int SZ = 1;
  for (auto v : xe[x]) {
    if (v == y) continue;
    pair<int, int> ret = dfs(v, x);
    SZ += ret.second;
    s.push_back(ret.first);
  }
  sort(s.begin(), s.end());
  int &v = mps[s];
  if (!v) {
    v = ++tot;
    sz_info[tot] = SZ;
    chd_info[tot] = s.size();
    chd_hash[tot] = s;
  }
  return pair<int, int>(v, SZ);
}
int m, n;
map<vector<int>, int> f, g;
int ans, ID[N];
vector<int> Has;
void calc(int x, int y) {
  int sn = 0;
  for (auto v : e[x]) {
    if (v == y) continue;
    calc(v, x);
    ++sn;
  }
  for (int stat = (int)(1); stat <= (int)(tot); stat++) {
    int tsz = chd_info[stat];
    if (sn < tsz) continue;
    int cnt = 0;
    f.clear();
    for (int i = (int)(0); i < (int)(tsz); i++) {
      int z = chd_hash[stat][i];
      if (ID[z] == -1) {
        ID[z] = cnt;
        Has.push_back(0);
        cnt++;
      }
      ++Has.back();
    }
    vector<int> NUL(cnt, 0);
    f[NUL] = 1;
    for (auto son : e[x]) {
      if (son == y) continue;
      g = f;
      for (int j = (int)(0); j < (int)(tsz); j++) {
        int v = chd_hash[stat][j], sv = v;
        if (j && v == chd_hash[stat][j - 1]) continue;
        v = ID[v];
        for (const auto &w : f) {
          auto st = w.first;
          int ways = (long long)w.second * dp[son][sv] % P;
          if (st[v] == Has[v]) continue;
          st[v]++;
          renew(g[st], ways);
        }
      }
      f = g;
    }
    dp[x][stat] = f[Has];
    if (sz_info[stat] == m) {
      renew(ans, dp[x][stat]);
    }
    Has.clear();
    for (int i = (int)(0); i < (int)(tsz); i++) ID[chd_hash[stat][i]] = -1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n - 1); i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  scanf("%d", &m);
  for (int i = (int)(1); i <= (int)(m - 1); i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    xe[x].push_back(y);
    xe[y].push_back(x);
  }
  for (int i = (int)(1); i <= (int)(m); i++) dfs(i, 0);
  memset(ID, -1, sizeof ID);
  calc(1, 0);
  printf("%d\n", ans);
  return 0;
}
