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;
const int mod = 1e9 + 7;
int i, j, k, n, m, s, t, ans1, ans2, ans;
int dp[1005][1 << 12];
struct Tree {
  int head[1005];
  struct edge {
    int vet, next;
  } E[2005];
  int son[1005];
  int num[1005];
  int a[1005][1005];
  int n, tot;
  Tree() {
    memset(head, -1, sizeof(head));
    tot = -1;
  }
  void add(int u, int v) {
    E[++tot] = (edge){v, head[u]};
    head[u] = tot;
  }
  void read() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      add(x, y);
      add(y, x);
    }
  }
  void dfs(int u, int dad) {
    son[u] = 0;
    for (int e = head[u]; e != -1; e = E[e].next)
      if (e != dad) {
        son[u] |= 1 << (E[e].vet - 1);
        dfs(E[e].vet, e ^ 1);
      }
  }
  void getsize(int u, int dad) {
    num[u] = 0;
    for (int e = head[u]; e != -1; e = E[e].next)
      if (e != dad) {
        a[u][++num[u]] = E[e].vet;
        getsize(E[e].vet, e ^ 1);
      }
  }
} S, T;
int powmod(const int &x, int y) {
  if (y == 0) return 1;
  int ret = powmod(x, y >> 1);
  if (y & 1)
    ret = (long long)ret * ret % mod * x % mod;
  else
    ret = (long long)ret * ret % mod;
  return ret;
}
int dfs(int u, int num, int opt) {
  if (num == 0) return opt == 0;
  int x = S.a[u][num];
  if (dp[x][opt] != -1) return dp[x][opt];
  dp[x][opt] = dfs(u, num - 1, opt);
  for (int i = 0; i < T.n; i++)
    if ((1 << i) & opt) {
      dp[x][opt] += (long long)dfs(u, num - 1, opt ^ (1 << i)) *
                    dfs(x, S.num[x], T.son[i + 1]) % mod;
      if (dp[x][opt] >= mod) dp[x][opt] -= mod;
    }
  return dp[x][opt];
}
int main() {
  S.read();
  T.read();
  S.getsize(1, -1);
  for (int j = 1; j <= T.n; j++) {
    memset(dp, -1, sizeof(dp));
    T.dfs(j, -1);
    for (int i = 1; i <= S.n; i++) {
      ans1 += dfs(i, S.num[i], T.son[j]);
      if (ans1 >= mod) ans1 -= mod;
    }
  }
  S = T;
  S.getsize(1, -1);
  for (int j = 1; j <= T.n; j++) {
    memset(dp, -1, sizeof(dp));
    T.dfs(j, -1);
    ans2 += dfs(1, S.num[1], T.son[j]);
    if (ans2 >= mod) ans2 -= mod;
  }
  ans = (long long)ans1 * powmod(ans2, mod - 2) % mod;
  printf("%d\n", ans);
  return 0;
}