#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using bs = basic_string<int>;
const ll Mod = 1e9 + 7;
map<string, int> mp, m1;
unordered_map<int, int> g[8010];
bs v[1010];
vector<bs> v1;
vector<vector<bs>> now[20];
ll s[20], ans, vl[20];
int n, m, _, siz[20];
ll f[1010][8010];
string h4sh(int p, int f, vector<bs>& v) {
  vector<string> sn;
  for (int i : v[p])
    if (i != f) sn.push_back(h4sh(i, p, v));
  sort(sn.begin(), sn.end());
  string re = "(";
  for (auto& s : sn) re += s;
  return re + ")";
}
void dfs(int p, int fa = 0) {
  f[p][1] = 1;
  for (int i : v[p])
    if (i != fa) {
      dfs(i, p);
      for (int j = _; j; j--)
        for (auto& [x, y] : g[j]) (f[p][y] += f[i][x] * f[p][j]) %= Mod;
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x] += y, v[y] += x;
  }
  scanf("%d", &m);
  now[1].push_back({{}, {}});
  mp["()"] = _ = 1;
  for (int i = 1; i < m; i++)
    for (auto& v : now[i]) {
      auto w = v;
      for (int j = 1; j <= i; j++) {
        w = v;
        w.push_back({j});
        w[j] += i + 1;
        string vl = h4sh(1, 0, w);
        if (!mp.count(vl)) mp[vl] = ++_, now[i + 1].push_back(w);
      }
    }
  for (int x = 1; x < m; x++)
    for (auto& u : now[x])
      for (int y = 1; y + x <= m; y++)
        for (auto& v : now[y]) {
          auto w = u;
          for (int i = 1; i <= y; i++) {
            w.push_back({});
            for (int j : v[i]) w.back() += j + x;
          }
          w[1] += x + 1;
          w[x + 1] += 1;
          g[mp[h4sh(1, 0, u)]][mp[h4sh(1, 0, v)]] = mp[h4sh(1, 0, w)];
        }
  dfs(1);
  v1.resize(m + 1);
  for (int i = 1, x, y; i < m; i++)
    scanf("%d%d", &x, &y), v1[x] += y, v1[y] += x;
  for (int i = 1; i <= m; i++) {
    string vl = h4sh(i, 0, v1);
    if (!m1.count(vl)) {
      m1[vl] = 1;
      for (int j = 1; j <= n; j++) (ans += f[j][mp[vl]]) %= Mod;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Develop a solution in Cpp to the problem described below:

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;
using ll = long long;
using bs = basic_string<int>;
const ll Mod = 1e9 + 7;
map<string, int> mp, m1;
unordered_map<int, int> g[8010];
bs v[1010];
vector<bs> v1;
vector<vector<bs>> now[20];
ll s[20], ans, vl[20];
int n, m, _, siz[20];
ll f[1010][8010];
string h4sh(int p, int f, vector<bs>& v) {
  vector<string> sn;
  for (int i : v[p])
    if (i != f) sn.push_back(h4sh(i, p, v));
  sort(sn.begin(), sn.end());
  string re = "(";
  for (auto& s : sn) re += s;
  return re + ")";
}
void dfs(int p, int fa = 0) {
  f[p][1] = 1;
  for (int i : v[p])
    if (i != fa) {
      dfs(i, p);
      for (int j = _; j; j--)
        for (auto& [x, y] : g[j]) (f[p][y] += f[i][x] * f[p][j]) %= Mod;
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x] += y, v[y] += x;
  }
  scanf("%d", &m);
  now[1].push_back({{}, {}});
  mp["()"] = _ = 1;
  for (int i = 1; i < m; i++)
    for (auto& v : now[i]) {
      auto w = v;
      for (int j = 1; j <= i; j++) {
        w = v;
        w.push_back({j});
        w[j] += i + 1;
        string vl = h4sh(1, 0, w);
        if (!mp.count(vl)) mp[vl] = ++_, now[i + 1].push_back(w);
      }
    }
  for (int x = 1; x < m; x++)
    for (auto& u : now[x])
      for (int y = 1; y + x <= m; y++)
        for (auto& v : now[y]) {
          auto w = u;
          for (int i = 1; i <= y; i++) {
            w.push_back({});
            for (int j : v[i]) w.back() += j + x;
          }
          w[1] += x + 1;
          w[x + 1] += 1;
          g[mp[h4sh(1, 0, u)]][mp[h4sh(1, 0, v)]] = mp[h4sh(1, 0, w)];
        }
  dfs(1);
  v1.resize(m + 1);
  for (int i = 1, x, y; i < m; i++)
    scanf("%d%d", &x, &y), v1[x] += y, v1[y] += x;
  for (int i = 1; i <= m; i++) {
    string vl = h4sh(i, 0, v1);
    if (!m1.count(vl)) {
      m1[vl] = 1;
      for (int j = 1; j <= n; j++) (ans += f[j][mp[vl]]) %= Mod;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

```