#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
unsigned long long ha[1000 + 5];
vector<unsigned long long> v[1000 + 5];
map<unsigned long long, int> mp, mp2;
int n, m, head[1000 + 5], Head[1000 + 5], fa[1000 + 5],
    cnt = 0, f[1000 + 5][15], F[1 << 12], size[1000 + 5], son[1000 + 5],
    q[1000 + 5], ans = 0, now, P[15];
struct edge {
  int to, next;
} e[1000 * 3 + 5];
inline void R(int& x, int y) {
  x += y;
  x >= 1000000007 ? x -= 1000000007 : 0;
}
inline void ins(int* H, int f, int t) {
  e[++cnt] = (edge){t, H[f]};
  H[f] = cnt;
  e[++cnt] = (edge){f, H[t]};
  H[t] = cnt;
}
int pow(int x, int k) {
  int sum = 1;
  for (; k; k >>= 1, x = 1LL * x * x % 1000000007)
    if (k & 1) sum = 1LL * sum * x % 1000000007;
  return sum;
}
void GetHa(int x, int f) {
  ha[x] = 0;
  size[x] = 1;
  v[x].clear();
  fa[x] = f;
  for (int i = Head[x]; i; i = e[i].next)
    if (e[i].to != f) {
      GetHa(e[i].to, x);
      v[x].push_back(ha[e[i].to]);
    }
  son[x] = v[x].size();
  sort(v[x].begin(), v[x].end());
  for (int i = 0; i < v[x].size(); ++i) ha[x] = ha[x] * 23333 + v[x][i];
  ha[x] = ha[x] * 2333 + size[x];
}
void Solve(int x, int fat) {
  int num = 0;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != fat) Solve(e[i].to, x), ++num;
  for (int i = 1; i <= m; ++i) {
    if (!son[i]) {
      f[x][i] = 1;
      continue;
    }
    if (num < son[i]) {
      f[x][i] = 0;
      continue;
    }
    for (int k = 0; k < 1 << son[i]; ++k) F[k] = 0;
    F[0] = 1;
    int top = 0;
    for (int j = Head[i]; j; j = e[j].next)
      if (e[j].to != fa[i]) q[++top] = e[j].to, ++mp2[ha[e[j].to]];
    for (int j = head[x]; j; j = e[j].next)
      if (e[j].to != fat) {
        for (int s = (1 << top) - 1; ~s; --s)
          for (int k = 1; k <= top; ++k)
            if (f[e[j].to][q[k]])
              if (!(s & (1 << k - 1)))
                R(F[s | (1 << k - 1)],
                  1LL * F[s] * f[e[j].to][q[k]] % 1000000007);
      }
    f[x][i] = F[(1 << top) - 1];
    for (int j = 1; j <= top; ++j) {
      if (mp2[ha[q[j]]])
        f[x][i] =
            1LL * f[x][i] * pow(P[mp2[ha[q[j]]]], 1000000007 - 2) % 1000000007,
        mp2[ha[q[j]]] = 0;
    }
  }
  R(ans, f[x][now]);
}
int main() {
  n = read();
  P[0] = 1;
  for (int i = 1; i <= 12; ++i) P[i] = 1LL * P[i - 1] * i % 1000000007;
  for (int i = 1; i < n; ++i) ins(head, read(), read());
  m = read();
  for (int i = 1; i < m; ++i) ins(Head, read(), read());
  for (int i = 1; i <= m; ++i) {
    GetHa(i, 0);
    if (mp[ha[i]]) continue;
    memset(f, 0, sizeof(f));
    mp[ha[i]] = 1;
    now = i;
    Solve(1, 0);
  }
  printf("%d\n", ans);
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
unsigned long long ha[1000 + 5];
vector<unsigned long long> v[1000 + 5];
map<unsigned long long, int> mp, mp2;
int n, m, head[1000 + 5], Head[1000 + 5], fa[1000 + 5],
    cnt = 0, f[1000 + 5][15], F[1 << 12], size[1000 + 5], son[1000 + 5],
    q[1000 + 5], ans = 0, now, P[15];
struct edge {
  int to, next;
} e[1000 * 3 + 5];
inline void R(int& x, int y) {
  x += y;
  x >= 1000000007 ? x -= 1000000007 : 0;
}
inline void ins(int* H, int f, int t) {
  e[++cnt] = (edge){t, H[f]};
  H[f] = cnt;
  e[++cnt] = (edge){f, H[t]};
  H[t] = cnt;
}
int pow(int x, int k) {
  int sum = 1;
  for (; k; k >>= 1, x = 1LL * x * x % 1000000007)
    if (k & 1) sum = 1LL * sum * x % 1000000007;
  return sum;
}
void GetHa(int x, int f) {
  ha[x] = 0;
  size[x] = 1;
  v[x].clear();
  fa[x] = f;
  for (int i = Head[x]; i; i = e[i].next)
    if (e[i].to != f) {
      GetHa(e[i].to, x);
      v[x].push_back(ha[e[i].to]);
    }
  son[x] = v[x].size();
  sort(v[x].begin(), v[x].end());
  for (int i = 0; i < v[x].size(); ++i) ha[x] = ha[x] * 23333 + v[x][i];
  ha[x] = ha[x] * 2333 + size[x];
}
void Solve(int x, int fat) {
  int num = 0;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != fat) Solve(e[i].to, x), ++num;
  for (int i = 1; i <= m; ++i) {
    if (!son[i]) {
      f[x][i] = 1;
      continue;
    }
    if (num < son[i]) {
      f[x][i] = 0;
      continue;
    }
    for (int k = 0; k < 1 << son[i]; ++k) F[k] = 0;
    F[0] = 1;
    int top = 0;
    for (int j = Head[i]; j; j = e[j].next)
      if (e[j].to != fa[i]) q[++top] = e[j].to, ++mp2[ha[e[j].to]];
    for (int j = head[x]; j; j = e[j].next)
      if (e[j].to != fat) {
        for (int s = (1 << top) - 1; ~s; --s)
          for (int k = 1; k <= top; ++k)
            if (f[e[j].to][q[k]])
              if (!(s & (1 << k - 1)))
                R(F[s | (1 << k - 1)],
                  1LL * F[s] * f[e[j].to][q[k]] % 1000000007);
      }
    f[x][i] = F[(1 << top) - 1];
    for (int j = 1; j <= top; ++j) {
      if (mp2[ha[q[j]]])
        f[x][i] =
            1LL * f[x][i] * pow(P[mp2[ha[q[j]]]], 1000000007 - 2) % 1000000007,
        mp2[ha[q[j]]] = 0;
    }
  }
  R(ans, f[x][now]);
}
int main() {
  n = read();
  P[0] = 1;
  for (int i = 1; i <= 12; ++i) P[i] = 1LL * P[i - 1] * i % 1000000007;
  for (int i = 1; i < n; ++i) ins(head, read(), read());
  m = read();
  for (int i = 1; i < m; ++i) ins(Head, read(), read());
  for (int i = 1; i <= m; ++i) {
    GetHa(i, 0);
    if (mp[ha[i]]) continue;
    memset(f, 0, sizeof(f));
    mp[ha[i]] = 1;
    now = i;
    Solve(1, 0);
  }
  printf("%d\n", ans);
  return 0;
}

```