#include <bits/stdc++.h>
using namespace std;
const int maxn = 2111, p = 1e9 + 7;
struct tree {
  int son[maxn][maxn], cnt[maxn], s[maxn], n;
  vector<int> v[maxn];
  void read() {
    cin >> n;
    for (int i = (1); i <= (n - 1); ++i) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
  }
  void dfs(int x, int fa) {
    cnt[x] = 0;
    s[x] = 0;
    for (int y : v[x]) {
      if (y == fa) continue;
      son[x][++cnt[x]] = y;
      s[x] |= (1 << (y - 1));
      dfs(y, x);
    }
  }
} a, b;
int f[maxn][5000];
int dp(int x, int now, int S) {
  if (!now || !S) return !S;
  int &ret = f[a.son[x][now]][S];
  if (ret) return ret - 1;
  ret = dp(x, now - 1, S);
  for (int y = a.son[x][now], k = 0; k < b.n; ++k)
    if (S >> k & 1)
      ret = (ret +
             1ll * dp(x, now - 1, S - (1 << k)) * dp(y, a.cnt[y], b.s[k + 1])) %
            p;
  return ret++;
}
int poww(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1ll * ans * a % p;
    b >>= 1;
    a = 1ll * a * a % p;
  }
  return ans;
}
int main() {
  a.read();
  b.read();
  a.dfs(1, 0);
  int ans1 = 0, ans2 = 0;
  for (int i = (1); i <= (b.n); ++i) {
    b.dfs(i, 0);
    for (int j = (1); j <= (a.n); ++j)
      ans1 = (ans1 + dp(j, a.cnt[j], b.s[i])) % p;
    memset(f, 0, sizeof(f));
  }
  a = b;
  a.dfs(1, 0);
  for (int i = (1); i <= (b.n); ++i) {
    b.dfs(i, 0);
    for (int j = (1); j <= (a.n); ++j)
      ans2 = (ans2 + dp(j, a.cnt[j], b.s[i])) % p;
    memset(f, 0, sizeof(f));
  }
  ans1 = 1ll * ans1 * poww(ans2, p - 2) % p;
  cout << ans1;
}

### Prompt

Create a solution in cpp for the following 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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2111, p = 1e9 + 7;
struct tree {
  int son[maxn][maxn], cnt[maxn], s[maxn], n;
  vector<int> v[maxn];
  void read() {
    cin >> n;
    for (int i = (1); i <= (n - 1); ++i) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
  }
  void dfs(int x, int fa) {
    cnt[x] = 0;
    s[x] = 0;
    for (int y : v[x]) {
      if (y == fa) continue;
      son[x][++cnt[x]] = y;
      s[x] |= (1 << (y - 1));
      dfs(y, x);
    }
  }
} a, b;
int f[maxn][5000];
int dp(int x, int now, int S) {
  if (!now || !S) return !S;
  int &ret = f[a.son[x][now]][S];
  if (ret) return ret - 1;
  ret = dp(x, now - 1, S);
  for (int y = a.son[x][now], k = 0; k < b.n; ++k)
    if (S >> k & 1)
      ret = (ret +
             1ll * dp(x, now - 1, S - (1 << k)) * dp(y, a.cnt[y], b.s[k + 1])) %
            p;
  return ret++;
}
int poww(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1ll * ans * a % p;
    b >>= 1;
    a = 1ll * a * a % p;
  }
  return ans;
}
int main() {
  a.read();
  b.read();
  a.dfs(1, 0);
  int ans1 = 0, ans2 = 0;
  for (int i = (1); i <= (b.n); ++i) {
    b.dfs(i, 0);
    for (int j = (1); j <= (a.n); ++j)
      ans1 = (ans1 + dp(j, a.cnt[j], b.s[i])) % p;
    memset(f, 0, sizeof(f));
  }
  a = b;
  a.dfs(1, 0);
  for (int i = (1); i <= (b.n); ++i) {
    b.dfs(i, 0);
    for (int j = (1); j <= (a.n); ++j)
      ans2 = (ans2 + dp(j, a.cnt[j], b.s[i])) % p;
    memset(f, 0, sizeof(f));
  }
  ans1 = 1ll * ans1 * poww(ans2, p - 2) % p;
  cout << ans1;
}

```