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>
#pragma GCC optimize("Ofast")
using namespace std;
long long mod = 1e9 + 7;
long long POW(long long x, long long y) {
  if (!y) return 1;
  long long t = POW(x, y / 2);
  if (y & 1) {
    return t * t % mod * x % mod;
  } else
    return t * t % mod;
}
long long inv(long long x) { return POW(x, mod - 2); }
struct tree {
  vector<long long> v[1005];
  long long ch[1005][1005], siz[1005], n, bit[1005];
  void init() {
    cin >> n;
    for (long long i = 1; i < n; i++) {
      long long x, y;
      cin >> x >> y;
      v[x].emplace_back(y);
      v[y].emplace_back(x);
    }
  }
  void dfs(long long x, long long fa) {
    bit[x] = 0;
    siz[x] = 0;
    for (auto u : v[x])
      if (u != fa) {
        ch[x][++siz[x]] = u;
        bit[x] |= 1 << (u - 1);
        dfs(u, x);
      }
  }
} second, T;
long long dp[1005][1 << 12];
long long dfs(long long fa, long long num, long long mask) {
  if (!num) return !mask;
  long long x = second.ch[fa][num];
  if (dp[x][mask] != -1) return dp[x][mask];
  long long re = dfs(fa, num - 1, mask);
  for (long long i = 0; i < T.n; i++)
    if (mask & (1 << i)) {
      re = (re + dfs(fa, num - 1, mask ^ (1 << i)) *
                     dfs(x, second.siz[x], T.bit[i + 1])) %
           mod;
    }
  return dp[x][mask] = re;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  second.init();
  T.init();
  second.dfs(1, -1);
  long long ans = 0, in = 0;
  for (long long i = 1; i <= T.n; i++) {
    memset(dp, -1, sizeof(dp));
    T.dfs(i, -1);
    for (long long j = 1; j <= second.n; j++)
      ans = (ans + dfs(j, second.siz[j], T.bit[i])) % mod;
  }
  second = T;
  second.dfs(1, -1);
  for (long long i = 1; i <= T.n; i++) {
    memset(dp, -1, sizeof(dp));
    T.dfs(i, -1);
    in = (in + dfs(1, second.siz[1], T.bit[i])) % mod;
  }
  cout << ans * inv(in) % mod;
  return 0;
}