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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1009, M = (1 << 12) + 9, mod = 1e9 + 7;
int son[N], ans, inv;
inline int read() {
  int res = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = (res << 1) + (res << 3) + ch - '0';
    ch = getchar();
  }
  return res * f;
}
inline int pls(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
struct Graph {
  int ver[N << 1], nxt[N << 1], h[N], tot;
  inline void add(int x, int y) { ver[++tot] = y, nxt[tot] = h[x], h[x] = tot; }
  void dfs(int u, int fa) {
    son[u] = 0;
    for (int i = h[u]; i; i = nxt[i]) {
      int v = ver[i];
      if (v == fa) continue;
      dfs(v, u);
      son[u] |= (1 << v - 1);
    }
  }
} S, T;
int n, m, f[N][M];
void dp(int u, int fa) {
  for (int i = 1; i < (1 << m); i++) f[u][i] = 0;
  f[u][0] = 1;
  for (int i = S.h[u]; i; i = S.nxt[i]) {
    int v = S.ver[i];
    if (v == fa) continue;
    dp(v, u);
    for (int S = (1 << m) - 1; S >= 0; S--)
      if (f[u][S])
        for (int i = 1; i <= m; i++)
          if (!(S & (1 << i - 1))) {
            int T = S | (1 << i - 1);
            f[u][T] = pls(f[u][T], 1ll * f[u][S] * f[v][son[i]] % mod);
          }
  }
}
inline int poww(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1) res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    k >>= 1;
  }
  return res;
}
int main() {
  n = read();
  for (int i = 1; i < n; i++) {
    int x, y;
    x = read(), y = read();
    S.add(x, y), S.add(y, x);
  }
  m = read();
  for (int i = 1; i < m; i++) {
    int x, y;
    x = read(), y = read();
    T.add(x, y), T.add(y, x);
  }
  for (int i = 1; i <= m; i++) {
    T.dfs(i, 0);
    dp(1, 0);
    for (int j = 1; j <= n; j++) ans = pls(ans, f[j][son[i]]);
  }
  S = T;
  for (int i = 1; i <= m; i++) {
    T.dfs(i, 0);
    dp(1, 0);
    inv = pls(inv, f[1][son[i]]);
  }
  printf("%d\n", 1ll * ans * poww(inv, mod - 2) % mod);
  return 0;
}