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 = (int)1e9 + 7;
const int MAXN = 1000, MAXM = 12;
int ipow(int a, int b) {
  if (b == 0) return 1;
  int ans = ipow(a, b / 2);
  ans = 1LL * ans * ans % MOD;
  if (b & 1) ans = 1LL * ans * a % MOD;
  return ans;
}
int modInv(int a) { return ipow(a, MOD - 2); }
map<tuple<int, int, int, int>, int> solve(vector<vector<int>> S,
                                          vector<vector<int>> T,
                                          bool full = false) {
  map<tuple<int, int, int, int>, int> MEM;
  int N = S.size() - 1, M = T.size() - 1;
  function<int(int, int, int, int)> solve = [&](int sa, int sp, int ta,
                                                int tp) {
    if (MEM.count({sa, sp, ta, tp})) return MEM[{sa, sp, ta, tp}];
    int K = 0;
    for (auto tx : T[ta])
      if (tx != tp) ++K;
    int DP[1 << MAXM];
    DP[0] = 1;
    for (int i = 1; i < (1 << K); ++i) DP[i] = 0;
    int cnt1 = 0;
    for (auto sx : S[sa])
      if (sx != sp) {
        ++cnt1;
        vector<int> child;
        for (auto tx : T[ta])
          if (tx != tp) child.push_back(solve(sx, sa, tx, ta));
        for (int i = (1 << K) - 1; i >= 0; --i)
          for (int j = 0; j < K; ++j)
            if (i & (1 << j))
              DP[i] = (DP[i] + 1LL * DP[i - (1 << j)] * child[j]) % MOD;
      }
    return MEM[{sa, sp, ta, tp}] = DP[(1 << K) - 1];
  };
  function<void(int, int)> dfs = [&](int a, int p) {
    for (int j = 1; j <= M; ++j) solve(a, p, j, 0);
    for (auto x : S[a])
      if (x != p) dfs(x, a);
  };
  dfs(1, 0);
  if (full)
    for (int i = 2; i <= N; ++i) dfs(i, 0);
  return MEM;
}
int main() {
  auto inp = []() {
    int N;
    scanf("%d", &N);
    vector<vector<int>> conn(N + 1);
    for (int i = 0; i < N - 1; ++i) {
      int u, v;
      scanf("%d%d", &u, &v);
      conn[u].push_back(v);
      conn[v].push_back(u);
    }
    return conn;
  };
  auto S = inp(), T = inp();
  int N = S.size() - 1, M = T.size() - 1;
  auto V1 = solve(S, T);
  auto V2 = solve(T, T, true);
  int ans = 0;
  for (int i = 1; i <= M; ++i) {
    bool flag = true;
    for (int j = 1; j < i; ++j)
      if (V2[{i, 0, j, 0}]) flag = false;
    if (flag) {
      int loc = 0;
      function<void(int, int)> dfs = [&](int a, int p) {
        loc += V1[{a, p, i, 0}];
        if (loc >= MOD) loc -= MOD;
        for (auto x : S[a])
          if (x != p) dfs(x, a);
      };
      dfs(1, 0);
      ans = (ans + 1LL * loc * modInv(V2[{i, 0, i, 0}])) % MOD;
    }
  }
  printf("%d\n", ans);
  return 0;
}