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 P = 131;
const int md = 1e9 + 7;
int n, m, xs;
int sz[15], f[2003][15], g[2003][1 << 12 | 3], st[15], prt[15], bin[15];
unsigned long long pw[15];
vector<int> e1[2003], e2[15];
struct Ha {
  static const int md = 2999999;
  unsigned long long key[md];
  void Ins(unsigned long long t) {
    int s = t % md;
    while (key[s] && key[s] != t)
      if (++s == md) s = 0;
    key[s] = t;
  }
  bool Check(unsigned long long t) {
    int s = t % md;
    while (key[s] && key[s] != t)
      if (++s == md) s = 0;
    return key[s] == t;
  }
} H;
inline int inc(int x, int y) { return x + y >= md ? x + y - md : x + y; }
inline int mul(int x, int y) { return (long long)x * y % md; }
inline void cinc(int& x, int y) { x = inc(x, y); }
inline void cmul(int& x, int y) { x = mul(x, y); }
inline int Qp(int x, int k, int m = 1) {
  for (; k; k >>= 1, cmul(x, x))
    if (k & 1) cmul(m, x);
  return m;
}
unsigned long long Dfs(int u, int fa) {
  sz[u] = 1, prt[u] = fa;
  vector<pair<unsigned long long, int> > ve;
  for (int v : e2[u]) {
    if (v == fa) continue;
    ve.emplace_back(Dfs(v, u), v);
    sz[u] += sz[v];
  }
  sort(ve.begin(), ve.end());
  unsigned long long h = '(';
  int cnt = 1;
  unsigned long long pre = 0;
  for (auto x : ve) {
    if (x.first == pre)
      ++cnt;
    else
      cnt = 1;
    h = h * pw[sz[x.second]] + x.first;
    pre = x.first;
    cmul(xs, cnt);
  }
  return h * P + ')';
}
void Dfs1(int u, int fa) {
  for (int v : e1[u]) {
    if (v == fa) continue;
    Dfs1(v, u);
  }
  for (int i = (1); i <= (m); ++i) {
    *st = 0;
    for (int v : e2[i])
      if (v != prt[i]) st[++*st] = v;
    g[0][0] = 1;
    int S = bin[*st] - 1, now = 0;
    for (int v : e1[u]) {
      ++now;
      for (int T = (0); T <= (S); ++T) g[now][T] = g[now - 1][T];
      for (int T = (0); T <= (S); ++T) {
        for (int k = (1); k <= (*st); ++k)
          if (T & bin[k - 1]) {
            cinc(g[now][T], mul(g[now - 1][T ^ bin[k - 1]], f[v][st[k]]));
          }
      }
    }
    cinc(f[u][i], g[now][S]);
    for (int j = (0); j <= (now); ++j)
      for (int T = (0); T <= (S); ++T) g[j][T] = 0;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = (2); i <= (n); ++i) {
    int x, y;
    cin >> x >> y;
    e1[x].emplace_back(y), e1[y].emplace_back(x);
  }
  cin >> m;
  for (int i = (2); i <= (m); ++i) {
    int x, y;
    cin >> x >> y;
    e2[x].emplace_back(y), e2[y].emplace_back(x);
  }
  pw[0] = 1;
  for (int i = (1); i <= (m); ++i) pw[i] = pw[i - 1] * P;
  bin[0] = 1;
  for (int i = (1); i <= (m); ++i) bin[i] = bin[i - 1] << 1;
  int ans = 0;
  for (int i = (1); i <= (m); ++i) {
    xs = 1;
    unsigned long long h = Dfs(i, 0);
    if (H.Check(h)) continue;
    H.Ins(h);
    Dfs1(1, 0);
    xs = Qp(xs, md - 2);
    for (int j = (1); j <= (n); ++j) cinc(ans, mul(xs, f[j][i]));
    for (int j = (0); j <= (n); ++j)
      for (int k = (0); k <= (m); ++k) f[j][k] = 0;
  }
  cout << ans << '\n';
  return 0;
}