#include <bits/stdc++.h>
using namespace std;
long long int modulo = 1e9 + 7;
long long int eleva(long long int b, long long int e) {
  if (e == 0) return 1;
  if (e % 2 == 0) return eleva(b * b % modulo, e / 2);
  return b * eleva(b * b % modulo, e / 2) % modulo;
}
long long int inverso(long long int x) { return eleva(x, modulo - 2); }
long long int factinv[13];
void reset(long long int amount[1 << 12], int n) {
  for (int i = 0; i < (1 << n); i++) amount[i] = 0;
}
void anyade(long long int &a, long long int b) { a = (a + b) % modulo; }
const int limite = 1150;
vector<int> g[limite], gg[limite];
int numnodos[limite];
long long int repesinv[limite];
long long int numencajes[limite][limite];
int numencajados[limite][limite];
int comparado[limite][limite];
int comparacion[limite][limite];
long long int amount[1 << 12];
long long int nextamount[1 << 12];
int computacomparacion(int u1, int u2) {
  int &c = comparacion[u1][u2];
  if (comparado[u1][u2]) return c;
  comparado[u1][u2] = 1;
  if (numnodos[u1] < numnodos[u2]) {
    c = -1;
    return c;
  }
  if (numnodos[u1] > numnodos[u2]) {
    c = 1;
    return c;
  }
  vector<int> &ar1 = g[u1];
  vector<int> &ar2 = g[u2];
  if (int(ar1.size()) < int(ar2.size())) {
    c = -1;
    return c;
  }
  if (int(ar1.size()) > int(ar2.size())) {
    c = 1;
    return c;
  }
  for (int i = 0; i < int(ar1.size()); i++) {
    int v1 = ar1[i];
    int v2 = ar2[i];
    c = computacomparacion(v1, v2);
    if (c) return c;
  }
  return c;
}
bool compara(int u, int v) { return computacomparacion(u, v) == -1; }
void ordena(int u) {
  vector<int> &ar = g[u];
  for (int i = 0; i < int(ar.size()); i++) ordena(ar[i]);
  sort(ar.begin(), ar.end(), compara);
  repesinv[u] = 1;
  int c = 0;
  for (int i = 0; i < int(ar.size()); i++) {
    c++;
    if (i == int(ar.size()) - 1 or compara(ar[i], ar[i + 1])) {
      repesinv[u] = repesinv[u] * factinv[c] % modulo;
      c = 0;
    }
  }
}
void computanumencajes(int u1, int u2) {
  long long int &num = numencajes[u1][u2];
  if (numencajados[u1][u2]) return;
  vector<int> &ar1 = g[u1];
  vector<int> &ar2 = g[u2];
  int n1 = int(ar1.size());
  int n2 = int(ar2.size());
  for (int i1 = 0; i1 < n1; i1++)
    for (int i2 = 0; i2 < n2; i2++) computanumencajes(ar1[i1], ar2[i2]);
  reset(amount, n2);
  amount[0] = 1;
  for (int i1 = 0; i1 < n1; i1++) {
    int v1 = ar1[i1];
    for (int c = (1 << n2) - 1; c >= 0; c--) {
      for (int i2 = 0; i2 < n2; i2++) {
        int v2 = ar2[i2];
        if ((c & (1 << i2)) == 0)
          anyade(amount[c | (1 << i2)], amount[c] * numencajes[v1][v2]);
      }
    }
  }
  num = amount[(1 << n2) - 1] * repesinv[u2] % modulo;
}
void eliminapadre(int u, int p) {
  vector<int> &ar = g[u];
  vector<int> nextar;
  for (int i = 0; i < int(ar.size()); i++) {
    int v = ar[i];
    if (v != p) {
      nextar.push_back(v);
      eliminapadre(v, u);
    }
  }
  ar = nextar;
}
void computanumnodos(int u) {
  numnodos[u] = 1;
  vector<int> &ar = g[u];
  for (int i = 0; i < int(ar.size()); i++) {
    int v = ar[i];
    computanumnodos(v);
    numnodos[u] += numnodos[v];
  }
}
int numpos = 1002;
int colgar(int u, int p) {
  int w = numpos++;
  vector<int> &arw = g[w];
  vector<int> &ar = gg[u];
  for (int i = 0; i < int(ar.size()); i++) {
    int v = ar[i];
    if (v != p) arw.push_back(colgar(v, u));
  }
  return w;
}
void escribe(string s, int u) {
  cout << s << "x" << endl;
  vector<int> &ar = g[u];
  for (int i = 0; i < int(ar.size()); i++) escribe(s + "  ", ar[i]);
}
void escribe(int u) { escribe("  ", u); }
int main() {
  factinv[0] = 1;
  for (int i = 1; i < 13; i++)
    factinv[i] = factinv[i - 1] * inverso(i) % modulo;
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  eliminapadre(1, 0);
  vector<int> &ar2 = g[1001];
  {
    int m;
    cin >> m;
    for (int i = 0; i < m - 1; i++) {
      int u, v;
      cin >> u >> v;
      gg[u].push_back(v);
      gg[v].push_back(u);
    }
    for (int u = 1; u <= m; u++) {
      ar2.push_back(colgar(u, 0));
      computanumnodos(ar2.back());
    }
  }
  long long int sol = 0;
  ordena(1001);
  for (int u1 = 1; u1 <= n; u1++) {
    for (int i2 = 0; i2 < int(ar2.size()); i2++) {
      if (i2 > 0 and compara(ar2[i2 - 1], ar2[i2]) == 0) continue;
      int u2 = ar2[i2];
      computanumencajes(u1, u2);
      anyade(sol, numencajes[u1][u2]);
    }
  }
  cout << sol << endl;
}
