#include <bits/stdc++.h>
const int p = 1000000007;
int c[12][15], e[12][15], g[12][5000], h[12][5000];
int edge1[3000], next1[3000], first1[3000], deg1[3000], sum_edge1;
int edge2[30], next2[30], first2[30], deg2[30], sum_edge2;
int i, n1, n2, r, s, t, x, y;
inline int power(int x, int y) {
  int s = 1;
  for (int i = 30; i >= 0; i--) {
    s = 1LL * s * s % p;
    if (y & (1 << i)) s = 1LL * s * x % p;
  }
  return s;
}
inline void addedge1(int x, int y) {
  sum_edge1++, edge1[sum_edge1] = y, next1[sum_edge1] = first1[x],
               first1[x] = sum_edge1, deg1[x]++;
  return;
}
inline void addedge2(int x, int y) {
  sum_edge2++, edge2[sum_edge2] = y, next2[sum_edge2] = first2[x],
               first2[x] = sum_edge2, deg2[x]++;
  return;
}
inline int dfs1(int d, int x, int fx, int y, int fy) {
  if (deg1[x] < deg2[y]) return 0;
  int l = 0;
  for (int j = 1; j <= n2; j++) e[d][j] = 0;
  for (int j = first2[y]; j != 0; j = next2[j])
    if (edge2[j] != fy) l++, e[d][edge2[j]] = l;
  for (int i = 0; i < (1 << l); i++) g[d][i] = 0;
  g[d][0] = 1;
  for (int i = first1[x]; i != 0; i = next1[i])
    if (edge1[i] != fx) {
      for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j];
      for (int j = first2[y]; j != 0; j = next2[j])
        if (edge2[j] != fy)
          c[d][e[d][edge2[j]]] = dfs1(d + 1, edge1[i], x, edge2[j], y);
      for (int k = 1; k <= l; k++)
        if (c[d][k])
          for (int j = 0; j < (1 << l); j++)
            if (!(j & (1 << (k - 1))))
              g[d][j | (1 << (k - 1))] =
                  (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p;
    }
  return g[d][(1 << l) - 1];
}
inline int dfs2(int d, int x, int fx, int y, int fy) {
  if (deg2[x] < deg2[y]) return 0;
  int l = 0;
  for (int j = 1; j <= n2; j++) e[d][j] = 0;
  for (int j = first2[y]; j != 0; j = next2[j])
    if (edge2[j] != fy) l++, e[d][edge2[j]] = l;
  for (int i = 0; i < (1 << l); i++) g[d][i] = 0;
  g[d][0] = 1;
  for (int i = first2[x]; i != 0; i = next2[i])
    if (edge2[i] != fx) {
      for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j];
      for (int j = first2[y]; j != 0; j = next2[j])
        if (edge2[j] != fy)
          c[d][e[d][edge2[j]]] = dfs2(d + 1, edge2[i], x, edge2[j], y);
      for (int k = 1; k <= l; k++)
        if (c[d][k])
          for (int j = 0; j < (1 << l); j++)
            if (!(j & (1 << (k - 1))))
              g[d][j | (1 << (k - 1))] =
                  (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p;
    }
  return g[d][(1 << l) - 1];
}
inline void input() {
  scanf("%d", &n1);
  for (i = 1; i < n1; i++) {
    scanf("%d%d", &x, &y);
    addedge1(x, y), addedge1(y, x);
  }
  scanf("%d", &n2);
  for (i = 1; i < n2; i++) {
    scanf("%d%d", &x, &y);
    addedge2(x, y), addedge2(y, x);
  }
  for (i = 1; i <= n2; i++)
    if (deg2[i] > deg2[r]) r = i;
  return;
}
int main() {
  input();
  for (i = 1; i <= n1; i++) s = (s + dfs1(0, i, 0, r, 0)) % p;
  for (i = 1; i <= n2; i++) t = t + dfs2(0, i, 0, r, 0);
  printf("%d", 1LL * s * power(t, p - 2) % p);
  return 0;
}
