#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
const int M = 12;
int mod = 1e9 + 7, n, m, u, v, ans;
vector<int> g[N], h[M + 20];
map<int, vector<int> > mp;
map<int, int> dp[N];
namespace {
inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline int sub(int a, int b) { return (a -= b) < 0 ? a + mod : a; }
inline int mul(int a, int b) { return (long long)a * b % mod; }
inline void upd(int &a, int b) { a = add(a, b); }
inline void dec(int &a, int b) { a = sub(a, b); }
}  // namespace
inline int bit(int x) { return 32 - __builtin_clz(x); }
inline int comb(int x, int y) { return x << bit(y) | y; }
int Ha(int u, int f = 0) {
  vector<int> s;
  s.clear();
  for (auto v : h[u]) {
    if (v == f) continue;
    s.push_back(Ha(v, u));
  }
  sort(s.begin(), s.end());
  int ret = 1;
  for (auto x : s) ret = comb(ret, x);
  ret <<= 1;
  if (!mp.count(ret)) mp[ret] = s;
  return ret;
}
void dfs(int x, int f = 0) {
  vector<int> ch;
  ch.clear();
  for (auto y : g[x]) {
    if (y == f) continue;
    dfs(y, x);
    ch.push_back(y);
  }
  for (const auto &u : mp) {
    auto &v = u.second;
    int sz = v.size();
    static int cur[1 << M | 10];
    memset(cur, 0, sizeof(int) * (1 << sz));
    cur[0] = 1;
    for (auto y : ch) {
      static int tmp[1 << M | 10];
      memcpy(tmp, cur, sizeof(int) * (1 << sz));
      for (int i = 0; i < sz; ++i) {
        int num = dp[y][v[i]];
        if (!num) continue;
        for (int used = 0; used < 1 << sz; ++used) {
          if ((used >> i & 1) || !cur[used]) continue;
          if (i && v[i - 1] == v[i] && !(used >> (i - 1) & 1)) continue;
          upd(tmp[used | (1 << i)], mul(num, cur[used]));
        }
      }
      memcpy(cur, tmp, sizeof(int) * (1 << sz));
    }
    if (cur[(1 << sz) - 1]) {
      dp[x][u.first] = cur[(1 << sz) - 1];
      if (bit(u.first) == m * 2) upd(ans, dp[x][u.first]);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  scanf("%d", &m);
  for (int i = 1; i < m; ++i) {
    scanf("%d%d", &u, &v);
    h[u].push_back(v), h[v].push_back(u);
  }
  for (int i = 1; i <= m; ++i) Ha(i);
  dfs(1);
  printf("%d\n", ans);
  return 0;
}

### Prompt

Please provide a CPP coded solution 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;
const int N = 2e3 + 5;
const int M = 12;
int mod = 1e9 + 7, n, m, u, v, ans;
vector<int> g[N], h[M + 20];
map<int, vector<int> > mp;
map<int, int> dp[N];
namespace {
inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline int sub(int a, int b) { return (a -= b) < 0 ? a + mod : a; }
inline int mul(int a, int b) { return (long long)a * b % mod; }
inline void upd(int &a, int b) { a = add(a, b); }
inline void dec(int &a, int b) { a = sub(a, b); }
}  // namespace
inline int bit(int x) { return 32 - __builtin_clz(x); }
inline int comb(int x, int y) { return x << bit(y) | y; }
int Ha(int u, int f = 0) {
  vector<int> s;
  s.clear();
  for (auto v : h[u]) {
    if (v == f) continue;
    s.push_back(Ha(v, u));
  }
  sort(s.begin(), s.end());
  int ret = 1;
  for (auto x : s) ret = comb(ret, x);
  ret <<= 1;
  if (!mp.count(ret)) mp[ret] = s;
  return ret;
}
void dfs(int x, int f = 0) {
  vector<int> ch;
  ch.clear();
  for (auto y : g[x]) {
    if (y == f) continue;
    dfs(y, x);
    ch.push_back(y);
  }
  for (const auto &u : mp) {
    auto &v = u.second;
    int sz = v.size();
    static int cur[1 << M | 10];
    memset(cur, 0, sizeof(int) * (1 << sz));
    cur[0] = 1;
    for (auto y : ch) {
      static int tmp[1 << M | 10];
      memcpy(tmp, cur, sizeof(int) * (1 << sz));
      for (int i = 0; i < sz; ++i) {
        int num = dp[y][v[i]];
        if (!num) continue;
        for (int used = 0; used < 1 << sz; ++used) {
          if ((used >> i & 1) || !cur[used]) continue;
          if (i && v[i - 1] == v[i] && !(used >> (i - 1) & 1)) continue;
          upd(tmp[used | (1 << i)], mul(num, cur[used]));
        }
      }
      memcpy(cur, tmp, sizeof(int) * (1 << sz));
    }
    if (cur[(1 << sz) - 1]) {
      dp[x][u.first] = cur[(1 << sz) - 1];
      if (bit(u.first) == m * 2) upd(ans, dp[x][u.first]);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  scanf("%d", &m);
  for (int i = 1; i < m; ++i) {
    scanf("%d%d", &u, &v);
    h[u].push_back(v), h[v].push_back(u);
  }
  for (int i = 1; i <= m; ++i) Ha(i);
  dfs(1);
  printf("%d\n", ans);
  return 0;
}

```