Problem: You are given two trees (connected undirected acyclic graphs) S and T.

Count the number of subtrees (connected subgraphs) of S that are isomorphic to tree T. Since this number can get quite large, output it modulo 109 + 7.

Two subtrees of tree S are considered different, if there exists a vertex in S that belongs to exactly one of them.

Tree G is called isomorphic to tree H if there exists a bijection f from the set of vertices of G to the set of vertices of H that has the following property: if there is an edge between vertices A and B in tree G, then there must be an edge between vertices f(A) and f(B) in tree H. And vice versa — if there is an edge between vertices A and B in tree H, there must be an edge between f - 1(A) and f - 1(B) in tree G.

Input

The first line contains a single integer |S| (1 ≤ |S| ≤ 1000) — the number of vertices of tree S.

Next |S| - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ |S|) and describe edges of tree S.

The next line contains a single integer |T| (1 ≤ |T| ≤ 12) — the number of vertices of tree T.

Next |T| - 1 lines contain two integers xi and yi (1 ≤ xi, yi ≤ |T|) and describe edges of tree T.

Output

On the first line output a single integer — the answer to the given task modulo 109 + 7.

Examples

Input

5
1 2
2 3
3 4
4 5
3
1 2
2 3


Output

3


Input

3
2 3
3 1
3
1 2
1 3


Output

1


Input

7
1 2
1 3
1 4
1 5
1 6
1 7
4
4 1
4 2
4 3


Output

20


Input

5
1 2
2 3
3 4
4 5
4
4 1
4 2
4 3


Output

0

Solution (in CPP):

Solution:
#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;
}