#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1000 + 7;
const double pi = acos(-1.0);
const double eps = 0.0000000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline void pr2(T x, int k = 64) {
  int i;
  for (i = k - 1; i >= 0; i--) fprintf(stderr, "%d", (x >> i) & 1);
  putchar(' ');
}
template <typename T>
inline void add_(T &A, int B) {
  A += B;
  (A >= M) && (A -= M);
}
template <typename T>
inline void mul_(T &A, long long B) {
  (A *= B) %= M;
}
template <typename T>
inline void mod_(T &A, long long B = M) {
  A %= M;
}
template <typename T>
inline void max_(T &A, T B) {
  (A < B) && (A = B);
}
template <typename T>
inline void min_(T &A, T B) {
  (A > B) && (A = B);
}
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = (long long)a * a % M)
    if (b & 1) ret = (long long)ret * a % M;
  return ret;
}
int n, m;
char str[maxn];
int TaskA();
void Task_one() { TaskA(); }
void Task_T() {
  int T;
  scanf("%d", &T);
  while (T--) TaskA();
}
void Task_more_n() {
  while (~scanf("%d", &n)) TaskA();
}
void Task_more_n_m() {
  while (~scanf("%d%d", &n, &m)) TaskA();
}
void Task_more_string() {
  while (~scanf("%s", str)) TaskA();
}
struct tree {
  vector<int> edge[maxn];
  int subtree[maxn], fa[maxn], n;
  void read() {
    scanf("%d", &n);
    int i, u, v;
    for (i = 1; i <= n - 1; i++) {
      scanf("%d%d", &u, &v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    }
  }
  void dfs_fa(int x, int father) {
    fa[x] = father;
    for (int v : edge[x])
      if (v != fa[x]) dfs_fa(v, x);
  }
  void dfs_subtree(int x, int fa) {
    subtree[x] = 0;
    for (int v : edge[x])
      if (v != fa) dfs_subtree(v, x), subtree[x] |= 1 << (v - 1);
  }
} S, T;
int val[1007][1 << 12 | 7];
int dfs(int x, int pos, int status) {
  if (pos == (int)S.edge[x].size()) return !status;
  if (S.edge[x][pos] == S.fa[x]) return dfs(x, pos + 1, status);
  int y = S.edge[x][pos];
  if (val[y][status] != -1) return val[y][status];
  int &ret = val[y][status];
  ret = 0;
  int i;
  add_(ret, dfs(x, pos + 1, status));
  for (i = 0; i < T.n; i++)
    if ((status >> i) & 1) {
      add_(ret, (long long)dfs(x, pos + 1, status ^ (1 << i)) *
                    dfs(y, 0, T.subtree[i + 1]) % M);
    }
  return ret;
}
int TaskA() {
  S.read();
  T.read();
  int i, j, ans = 0, inv = 0;
  S.dfs_fa(1, 0);
  for (i = 1; i <= T.n; i++) {
    T.dfs_subtree(i, 0);
    memset(val, 0xff, sizeof(val));
    for (j = 1; j <= S.n; j++) add_(ans, dfs(j, 0, T.subtree[i]));
  }
  S = T;
  S.dfs_fa(1, 0);
  for (i = 1; i <= T.n; i++) {
    T.dfs_subtree(i, 0);
    memset(val, 0xff, sizeof(val));
    for (j = 1; j <= S.n; j++) add_(inv, dfs(j, 0, T.subtree[i]));
  }
  printf("%I64d\n", ans * powMM((long long)inv, M - 2) % M);
  return 0;
}
void initialize() {}
int main() {
  int startTime = clock();
  initialize();
  fprintf(stderr, "/--- initializeTime: %ld milliseconds ---/\n",
          clock() - startTime);
  Task_one();
}

### Prompt

Your challenge is to write 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;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1000 + 7;
const double pi = acos(-1.0);
const double eps = 0.0000000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline void pr2(T x, int k = 64) {
  int i;
  for (i = k - 1; i >= 0; i--) fprintf(stderr, "%d", (x >> i) & 1);
  putchar(' ');
}
template <typename T>
inline void add_(T &A, int B) {
  A += B;
  (A >= M) && (A -= M);
}
template <typename T>
inline void mul_(T &A, long long B) {
  (A *= B) %= M;
}
template <typename T>
inline void mod_(T &A, long long B = M) {
  A %= M;
}
template <typename T>
inline void max_(T &A, T B) {
  (A < B) && (A = B);
}
template <typename T>
inline void min_(T &A, T B) {
  (A > B) && (A = B);
}
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = (long long)a * a % M)
    if (b & 1) ret = (long long)ret * a % M;
  return ret;
}
int n, m;
char str[maxn];
int TaskA();
void Task_one() { TaskA(); }
void Task_T() {
  int T;
  scanf("%d", &T);
  while (T--) TaskA();
}
void Task_more_n() {
  while (~scanf("%d", &n)) TaskA();
}
void Task_more_n_m() {
  while (~scanf("%d%d", &n, &m)) TaskA();
}
void Task_more_string() {
  while (~scanf("%s", str)) TaskA();
}
struct tree {
  vector<int> edge[maxn];
  int subtree[maxn], fa[maxn], n;
  void read() {
    scanf("%d", &n);
    int i, u, v;
    for (i = 1; i <= n - 1; i++) {
      scanf("%d%d", &u, &v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    }
  }
  void dfs_fa(int x, int father) {
    fa[x] = father;
    for (int v : edge[x])
      if (v != fa[x]) dfs_fa(v, x);
  }
  void dfs_subtree(int x, int fa) {
    subtree[x] = 0;
    for (int v : edge[x])
      if (v != fa) dfs_subtree(v, x), subtree[x] |= 1 << (v - 1);
  }
} S, T;
int val[1007][1 << 12 | 7];
int dfs(int x, int pos, int status) {
  if (pos == (int)S.edge[x].size()) return !status;
  if (S.edge[x][pos] == S.fa[x]) return dfs(x, pos + 1, status);
  int y = S.edge[x][pos];
  if (val[y][status] != -1) return val[y][status];
  int &ret = val[y][status];
  ret = 0;
  int i;
  add_(ret, dfs(x, pos + 1, status));
  for (i = 0; i < T.n; i++)
    if ((status >> i) & 1) {
      add_(ret, (long long)dfs(x, pos + 1, status ^ (1 << i)) *
                    dfs(y, 0, T.subtree[i + 1]) % M);
    }
  return ret;
}
int TaskA() {
  S.read();
  T.read();
  int i, j, ans = 0, inv = 0;
  S.dfs_fa(1, 0);
  for (i = 1; i <= T.n; i++) {
    T.dfs_subtree(i, 0);
    memset(val, 0xff, sizeof(val));
    for (j = 1; j <= S.n; j++) add_(ans, dfs(j, 0, T.subtree[i]));
  }
  S = T;
  S.dfs_fa(1, 0);
  for (i = 1; i <= T.n; i++) {
    T.dfs_subtree(i, 0);
    memset(val, 0xff, sizeof(val));
    for (j = 1; j <= S.n; j++) add_(inv, dfs(j, 0, T.subtree[i]));
  }
  printf("%I64d\n", ans * powMM((long long)inv, M - 2) % M);
  return 0;
}
void initialize() {}
int main() {
  int startTime = clock();
  initialize();
  fprintf(stderr, "/--- initializeTime: %ld milliseconds ---/\n",
          clock() - startTime);
  Task_one();
}

```