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;
int A[5010][5010], B[30][30], n, m, ans;
int cnt, hs[5010];
map<pair<int, int>, int> mp;
bool Cmph(int a, int b) { return (hs[a] < hs[b]); }
inline void Get_hash(int x, int y) {
  int nm0 = 0, nm1 = 0;
  for (int i = 1; i <= B[x][0]; i++)
    if (B[x][i] != y) Get_hash(B[x][i], x);
  sort(B[x] + 1, B[x] + 1 + B[x][0], Cmph);
  for (int i = 1; i <= B[x][0]; i++)
    if (B[x][i] != y) {
      nm0 = (nm0 * 1ll * 2333 % 1000000007 + hs[B[x][i]]) % 1000000007;
      nm1 = (nm1 * 1ll * 3666 % 1000000009 + hs[B[x][i]]) % 1000000009;
    }
  if (!mp[make_pair(nm0, nm1)]) mp[make_pair(nm0, nm1)] = ++cnt;
  hs[x] = mp[make_pair(nm0, nm1)];
}
int fa[5010], gg[5010], g[5010], f[5010][30], pr[30];
inline void Dfs(int x) {
  for (int i = 1; i <= A[x][0]; i++)
    if (A[x][i] != fa[x]) fa[A[x][i]] = x, Dfs(A[x][i]);
}
inline void Add(int &a, int b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
}
inline void Dp(int x, int y, int fy) {
  for (int i = 1; i <= A[x][0]; i++)
    if (A[x][i] != fa[x])
      for (int j = 1; j <= B[y][0]; j++)
        if (B[y][j] != fy) Dp(A[x][i], B[y][j], y);
  int h = 0;
  memset(pr, 0, sizeof(pr));
  for (int i = 1; i <= B[y][0]; i++)
    if (B[y][i] != fy) {
      h |= 1 << i - 1;
      for (int j = 1; j < i; j++)
        if (B[y][j] != fy && hs[B[y][j]] == hs[B[y][i]]) pr[i] = j;
    }
  memset(g, 0, sizeof(g[0]) * (h + 1));
  g[0] = 1;
  for (int i = 1; i <= A[x][0]; i++)
    if (A[x][i] != fa[x]) {
      memset(gg, 0, sizeof(gg[0]) * (h + 1));
      for (int j = 0; j < h; j++) gg[j] = g[j];
      for (int j = 1; j <= h; j++)
        for (int k = 1; k <= B[y][0]; k++)
          if (B[y][k] != fy && (j & (1 << k - 1))) {
            if (pr[k] && !(j & (1 << pr[k] - 1))) continue;
            int nm = f[A[x][i]][B[y][k]];
            Add(g[j], gg[j ^ (1 << k - 1)] * 1ll * nm % 1000000007);
          }
    }
  f[x][y] = g[h];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    A[u][++A[u][0]] = v, A[v][++A[v][0]] = u;
  }
  scanf("%d", &m);
  for (int i = 1; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    B[u][++B[u][0]] = v, B[v][++B[v][0]] = u;
  }
  Dfs(1);
  for (int i = 1; i <= m; i++) {
    int old = cnt;
    Get_hash(i, 0);
    if (hs[i] <= old) continue;
    for (int j = 1; j <= n; j++) {
      Dp(j, i, 0);
      Add(ans, f[j][i]);
    }
  }
  printf("%d\n", ans);
}