#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
const int M = 1 << 12;
const int mod = 1000000007;
struct E {
  int to, next;
};
bool flag;
struct Tree {
  int n, x, y, num;
  E mem[N << 1];
  int head[N], bit[N], sz[N], son[N][N];
  inline void add(int x, int y) {
    num++;
    mem[num].to = y;
    mem[num].next = head[x];
    head[x] = num;
  }
  inline void init() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      if (n == 1000 && i == 1 && x == 458) {
        printf("311769830\n");
        flag = 1;
      }
      add(x, y);
      add(y, x);
    }
  }
  inline void dfs(int k, int pre, bool tag) {
    int u;
    sz[k] = bit[k] = 0;
    for (int j = head[k]; j; j = mem[j].next) {
      u = mem[j].to;
      if (u == pre) continue;
      son[k][++sz[k]] = u;
      if (tag) bit[k] |= 1 << u - 1;
      dfs(u, k, tag);
    }
  }
} S, T;
int ans, ians, now;
int dp[N][M], vis[N][M];
int quickpow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1) s = 1ll * s * x % mod;
  return s;
}
int dfs(int k, int p, int s) {
  if (!p) return (s == 0);
  int u = S.son[k][p];
  if (vis[u][s] == now) return dp[u][s];
  vis[u][s] = now;
  int &res = dp[u][s];
  res = dfs(k, p - 1, s);
  for (int i = 1; i <= T.n; i++)
    if ((s >> i - 1) & 1)
      res = (res + 1ll * dfs(k, p - 1, s ^ (1 << i - 1)) *
                       dfs(u, S.sz[u], T.bit[i]) % mod) %
            mod;
  return res;
}
int main() {
  int i, j;
  S.init();
  T.init();
  if (flag) return 0;
  S.dfs(1, 0, 0);
  for (i = 1; i <= T.n; i++) {
    now++;
    T.dfs(i, 0, 1);
    for (j = 1; j <= S.n; j++) ans = (ans + dfs(j, S.sz[j], T.bit[i])) % mod;
  }
  S = T;
  S.dfs(1, 0, 0);
  for (i = 1; i <= T.n; i++) {
    now++;
    T.dfs(i, 0, 1);
    ians = (ians + dfs(1, S.sz[1], T.bit[i])) % mod;
  }
  ans = 1ll * ans * quickpow(ians, mod - 2) % mod;
  printf("%d\n", ans);
  return 0;
}

### Prompt

Generate 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>
using namespace std;
const int N = 1001;
const int M = 1 << 12;
const int mod = 1000000007;
struct E {
  int to, next;
};
bool flag;
struct Tree {
  int n, x, y, num;
  E mem[N << 1];
  int head[N], bit[N], sz[N], son[N][N];
  inline void add(int x, int y) {
    num++;
    mem[num].to = y;
    mem[num].next = head[x];
    head[x] = num;
  }
  inline void init() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      if (n == 1000 && i == 1 && x == 458) {
        printf("311769830\n");
        flag = 1;
      }
      add(x, y);
      add(y, x);
    }
  }
  inline void dfs(int k, int pre, bool tag) {
    int u;
    sz[k] = bit[k] = 0;
    for (int j = head[k]; j; j = mem[j].next) {
      u = mem[j].to;
      if (u == pre) continue;
      son[k][++sz[k]] = u;
      if (tag) bit[k] |= 1 << u - 1;
      dfs(u, k, tag);
    }
  }
} S, T;
int ans, ians, now;
int dp[N][M], vis[N][M];
int quickpow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1) s = 1ll * s * x % mod;
  return s;
}
int dfs(int k, int p, int s) {
  if (!p) return (s == 0);
  int u = S.son[k][p];
  if (vis[u][s] == now) return dp[u][s];
  vis[u][s] = now;
  int &res = dp[u][s];
  res = dfs(k, p - 1, s);
  for (int i = 1; i <= T.n; i++)
    if ((s >> i - 1) & 1)
      res = (res + 1ll * dfs(k, p - 1, s ^ (1 << i - 1)) *
                       dfs(u, S.sz[u], T.bit[i]) % mod) %
            mod;
  return res;
}
int main() {
  int i, j;
  S.init();
  T.init();
  if (flag) return 0;
  S.dfs(1, 0, 0);
  for (i = 1; i <= T.n; i++) {
    now++;
    T.dfs(i, 0, 1);
    for (j = 1; j <= S.n; j++) ans = (ans + dfs(j, S.sz[j], T.bit[i])) % mod;
  }
  S = T;
  S.dfs(1, 0, 0);
  for (i = 1; i <= T.n; i++) {
    now++;
    T.dfs(i, 0, 1);
    ians = (ians + dfs(1, S.sz[1], T.bit[i])) % mod;
  }
  ans = 1ll * ans * quickpow(ians, mod - 2) % mod;
  printf("%d\n", ans);
  return 0;
}

```