#include <bits/stdc++.h>
const int MX_n1 = 1e3 + 23;
const int MX_n2 = 12;
const long long MOD = 1e9 + 7;
int read() {
  char k = getchar();
  int x = 0;
  while (k < '0' || k > '9') k = getchar();
  while (k >= '0' && k <= '9') x = x * 10 + k - '0', k = getchar();
  return x;
}
long long qpow(long long a, long long b, long long p = MOD) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % p;
    a = a * a % p, b >>= 1;
  }
  return ans;
}
int T1[MX_n1], T2[MX_n2 + 1], tot;
struct edge {
  int node, next;
} h[(MX_n1 + MX_n2) << 1];
void addedge(int u, int v, int *head, int flg = 1) {
  h[++tot] = (edge){v, head[u]}, head[u] = tot;
  if (flg) addedge(v, u, head, false);
}
std::vector<int> e[MX_n1];
void gete(int x, int f, int *head) {
  e[x].clear();
  for (int i = head[x], d; i; i = h[i].next) {
    if ((d = h[i].node) == f) continue;
    e[x].push_back(d);
    gete(d, x, head);
  }
}
int bit[MX_n2 + 1], chcnt[MX_n2 + 1];
void getbit(int x, int f, int *head) {
  bit[x] = 0;
  for (int i = head[x], d; i; i = h[i].next) {
    if ((d = h[i].node) == f) continue;
    bit[x] |= 1 << (d - 1);
    getbit(d, x, head);
  }
}
int n1, n2;
long long dp[MX_n1][1 << MX_n2];
long long dapai(int x, int y, int st) {
  if (y == -1) return !st;
  int to = e[x][y];
  if (~dp[to][st]) return dp[to][st];
  long long &ans = dp[to][st];
  ans = dapai(x, y - 1, st);
  for (int i = 0; i < n2; ++i)
    if ((st >> i) & 1)
      ans = (ans + dapai(x, y - 1, st ^ (1 << i)) *
                       dapai(to, (int)e[to].size() - 1, bit[i + 1])) %
            MOD;
  return ans;
}
int main() {
  n1 = read();
  for (int i = 1, u, v; i < n1; ++i) {
    u = read(), v = read();
    addedge(u, v, T1);
  }
  n2 = read();
  for (int i = 1, u, v; i < n2; ++i) {
    u = read(), v = read();
    addedge(u, v, T2);
  }
  long long ans = 0;
  gete(1, 0, T1);
  for (int i = 1; i <= n2; ++i) {
    memset(dp, -1, sizeof dp);
    getbit(i, 0, T2);
    for (int j = 1; j <= n1; ++j) {
      ans = (ans + dapai(j, (int)e[j].size() - 1, bit[i])) % MOD;
    }
  }
  long long ans2 = 0;
  gete(1, 0, T2);
  for (int i = 1; i <= n2; ++i) {
    memset(dp, -1, sizeof dp);
    getbit(i, 0, T2);
    ans2 = (ans2 + dapai(1, (int)e[1].size() - 1, bit[i])) % MOD;
  }
  printf("%lld\n", ans * qpow(ans2, MOD - 2) % MOD);
  return 0;
}

### Prompt

Your challenge is to write a cpp solution to the following 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

### Response

```cpp
#include <bits/stdc++.h>
const int MX_n1 = 1e3 + 23;
const int MX_n2 = 12;
const long long MOD = 1e9 + 7;
int read() {
  char k = getchar();
  int x = 0;
  while (k < '0' || k > '9') k = getchar();
  while (k >= '0' && k <= '9') x = x * 10 + k - '0', k = getchar();
  return x;
}
long long qpow(long long a, long long b, long long p = MOD) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % p;
    a = a * a % p, b >>= 1;
  }
  return ans;
}
int T1[MX_n1], T2[MX_n2 + 1], tot;
struct edge {
  int node, next;
} h[(MX_n1 + MX_n2) << 1];
void addedge(int u, int v, int *head, int flg = 1) {
  h[++tot] = (edge){v, head[u]}, head[u] = tot;
  if (flg) addedge(v, u, head, false);
}
std::vector<int> e[MX_n1];
void gete(int x, int f, int *head) {
  e[x].clear();
  for (int i = head[x], d; i; i = h[i].next) {
    if ((d = h[i].node) == f) continue;
    e[x].push_back(d);
    gete(d, x, head);
  }
}
int bit[MX_n2 + 1], chcnt[MX_n2 + 1];
void getbit(int x, int f, int *head) {
  bit[x] = 0;
  for (int i = head[x], d; i; i = h[i].next) {
    if ((d = h[i].node) == f) continue;
    bit[x] |= 1 << (d - 1);
    getbit(d, x, head);
  }
}
int n1, n2;
long long dp[MX_n1][1 << MX_n2];
long long dapai(int x, int y, int st) {
  if (y == -1) return !st;
  int to = e[x][y];
  if (~dp[to][st]) return dp[to][st];
  long long &ans = dp[to][st];
  ans = dapai(x, y - 1, st);
  for (int i = 0; i < n2; ++i)
    if ((st >> i) & 1)
      ans = (ans + dapai(x, y - 1, st ^ (1 << i)) *
                       dapai(to, (int)e[to].size() - 1, bit[i + 1])) %
            MOD;
  return ans;
}
int main() {
  n1 = read();
  for (int i = 1, u, v; i < n1; ++i) {
    u = read(), v = read();
    addedge(u, v, T1);
  }
  n2 = read();
  for (int i = 1, u, v; i < n2; ++i) {
    u = read(), v = read();
    addedge(u, v, T2);
  }
  long long ans = 0;
  gete(1, 0, T1);
  for (int i = 1; i <= n2; ++i) {
    memset(dp, -1, sizeof dp);
    getbit(i, 0, T2);
    for (int j = 1; j <= n1; ++j) {
      ans = (ans + dapai(j, (int)e[j].size() - 1, bit[i])) % MOD;
    }
  }
  long long ans2 = 0;
  gete(1, 0, T2);
  for (int i = 1; i <= n2; ++i) {
    memset(dp, -1, sizeof dp);
    getbit(i, 0, T2);
    ans2 = (ans2 + dapai(1, (int)e[1].size() - 1, bit[i])) % MOD;
  }
  printf("%lld\n", ans * qpow(ans2, MOD - 2) % MOD);
  return 0;
}

```