#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn &a) {
  tn x = 0, f = 1;
  char c = ' ';
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  a = x * f;
}
const int mod = 1e9 + 7;
int vis[1010][1 << 12], f[1010][1 << 12];
struct tree {
  int g[1010], s[1010], n;
  vector<int> e[1010], son[1010];
  void init() {
    read(n);
    for (int i = 1; i < n; i++) {
      int x, y;
      read(x);
      read(y);
      e[x].push_back(y);
      e[y].push_back(x);
    }
  }
  void dfs(int x, int dad) {
    s[x] = 1;
    g[x] = 0;
    son[x].clear();
    for (int v : e[x])
      if (v != dad) {
        son[x].push_back(v);
        if (v < 20) g[x] |= 1 << v - 1;
        dfs(v, x);
        s[x] += s[v];
      }
  }
} S, T;
long long dp(int x, int now, int hsh) {
  if (!now) return !hsh;
  int p = S.son[x][now - 1];
  if (vis[p][hsh]) return f[p][hsh];
  vis[p][hsh] = 1;
  f[p][hsh] = dp(x, now - 1, hsh);
  for (int i = 0; i < T.n; i++)
    if (hsh >> i & 1)
      f[p][hsh] = (f[p][hsh] + dp(x, now - 1, hsh ^ 1 << i) *
                                   dp(p, S.son[p].size(), T.g[i + 1])) %
                  mod;
  return f[p][hsh];
}
long long fp(long long a, long long k) {
  long long ans = 1;
  for (; k; k >>= 1, a = a * a % mod)
    if (k & 1) ans = a * ans % mod;
  return ans;
}
int main() {
  S.init();
  T.init();
  S.dfs(1, 0);
  long long ans = 0, Inv = 0;
  for (int i = 1; i <= T.n; i++) {
    T.dfs(i, 0);
    memset(vis, 0, sizeof(vis));
    memset(f, 0, sizeof(f));
    for (int j = 1; j <= S.n; j++)
      ans = (ans + dp(j, S.son[j].size(), T.g[i])) % mod;
  }
  S = T;
  S.dfs(1, 0);
  for (int i = 1; i <= T.n; i++) {
    T.dfs(i, 0);
    memset(vis, 0, sizeof(vis));
    memset(f, 0, sizeof(f));
    Inv = (Inv + dp(1, S.son[1].size(), T.g[i])) % mod;
  }
  cout << (ans * fp(Inv, mod - 2) % mod + mod) % mod << '\n';
  return 0;
}

### Prompt

Please formulate a cpp solution to 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;
template <typename tn>
void read(tn &a) {
  tn x = 0, f = 1;
  char c = ' ';
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  a = x * f;
}
const int mod = 1e9 + 7;
int vis[1010][1 << 12], f[1010][1 << 12];
struct tree {
  int g[1010], s[1010], n;
  vector<int> e[1010], son[1010];
  void init() {
    read(n);
    for (int i = 1; i < n; i++) {
      int x, y;
      read(x);
      read(y);
      e[x].push_back(y);
      e[y].push_back(x);
    }
  }
  void dfs(int x, int dad) {
    s[x] = 1;
    g[x] = 0;
    son[x].clear();
    for (int v : e[x])
      if (v != dad) {
        son[x].push_back(v);
        if (v < 20) g[x] |= 1 << v - 1;
        dfs(v, x);
        s[x] += s[v];
      }
  }
} S, T;
long long dp(int x, int now, int hsh) {
  if (!now) return !hsh;
  int p = S.son[x][now - 1];
  if (vis[p][hsh]) return f[p][hsh];
  vis[p][hsh] = 1;
  f[p][hsh] = dp(x, now - 1, hsh);
  for (int i = 0; i < T.n; i++)
    if (hsh >> i & 1)
      f[p][hsh] = (f[p][hsh] + dp(x, now - 1, hsh ^ 1 << i) *
                                   dp(p, S.son[p].size(), T.g[i + 1])) %
                  mod;
  return f[p][hsh];
}
long long fp(long long a, long long k) {
  long long ans = 1;
  for (; k; k >>= 1, a = a * a % mod)
    if (k & 1) ans = a * ans % mod;
  return ans;
}
int main() {
  S.init();
  T.init();
  S.dfs(1, 0);
  long long ans = 0, Inv = 0;
  for (int i = 1; i <= T.n; i++) {
    T.dfs(i, 0);
    memset(vis, 0, sizeof(vis));
    memset(f, 0, sizeof(f));
    for (int j = 1; j <= S.n; j++)
      ans = (ans + dp(j, S.son[j].size(), T.g[i])) % mod;
  }
  S = T;
  S.dfs(1, 0);
  for (int i = 1; i <= T.n; i++) {
    T.dfs(i, 0);
    memset(vis, 0, sizeof(vis));
    memset(f, 0, sizeof(f));
    Inv = (Inv + dp(1, S.son[1].size(), T.g[i])) % mod;
  }
  cout << (ans * fp(Inv, mod - 2) % mod + mod) % mod << '\n';
  return 0;
}

```