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 = 2010, b1 = 123, b2 = 2333, P = 1e9 + 7;
int u, v, n, m, a[1 << 15], b[1 << 15], C[N][20], ans, fac[20];
set<unsigned int> mp;
int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1) ans = 1ll * ans * a % P;
  return ans;
}
struct Graph {
  int hed[N], to[N << 1], nex[N << 1], ed;
  Graph() {
    memset(hed, -1, sizeof(hed));
    ed = -1;
  }
  inline void add(int u, int v) {
    to[++ed] = v;
    nex[ed] = hed[u];
    hed[u] = ed;
  }
} G, H;
namespace T {
int to[20], siz[20], q[20], vis[20], tot, S;
unsigned int f[20], fxk;
inline void cls() { memset(to, 0, sizeof(to)); }
inline void add(int u, int v) { to[u] |= 1 << v; }
void dfs(int u) {
  siz[u] = 1;
  f[u] = 0;
  for (int i = to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
    dfs(v), siz[u] += siz[v];
  tot = 0;
  for (int i = to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
    q[++tot] = v;
  sort(q + 1, q + tot + 1, [&](int x, int y) { return f[x] < f[y]; });
  f[u] = siz[u];
  for (int i = (1); i <= (tot); ++i) f[u] = f[u] * b1 + f[q[i]];
  f[u] *= b2 * siz[u];
  S = 0;
  for (int i = (1); i <= (tot); ++i)
    (f[q[i]] > 1 && f[q[i]] == f[q[i - 1]]) ? (vis[q[i]] = S, S |= 1 << q[i])
                                            : (vis[q[i]] = 0, S = 1 << q[i]);
}
}  // namespace T
void dfs(int u, int fa) {
  for (int i = H.hed[u], v = H.to[i]; ~i; i = H.nex[i], v = H.to[i])
    if (v ^ fa) T::add(u, v), dfs(v, u);
}
int bo[20], g[1 << 15], lf[20], dp[N][20];
void DFS(int u, int fa) {
  int tot = 0;
  for (int i = G.hed[u], v = G.to[i]; ~i; i = G.nex[i], v = G.to[i])
    if (v ^ fa) DFS(v, u), ++tot;
  for (int i = (0); i <= (m - 1); ++i) {
    if (bo[i]) continue;
    if (tot < a[T::to[i]]) {
      dp[u][i] = 0;
      continue;
    }
    for (int j = T::to[i]; j; j = (j - 1) & T::to[i]) g[j] = 0;
    g[0] = 1;
    for (int j = G.hed[u], v = G.to[j]; ~j; j = G.nex[j], v = G.to[j])
      if (v ^ fa)
        for (int k = T::to[i]; k; k = (k - 1) & T::to[i])
          for (int p = k; p; p -= p & (-p))
            if ((T::vis[b[p & (-p)]] & k) == T::vis[b[p & (-p)]])
              g[k] = (g[k] + 1ll * dp[v][b[p & (-p)]] * g[k ^ (p & (-p))]) % P;
    dp[u][i] = 1ll * g[T::to[i]] * C[tot - a[T::to[i]]][lf[i]] % P;
  }
}
int times(int rt) {
  for (int i = (0); i <= (m - 1); ++i) bo[i] = T::siz[i] == 1, lf[i] = 0;
  for (int u = (0); u <= (m - 1); ++u)
    for (int i = T::to[u], v = b[i & (-i)]; i; i -= i & (-i), v = b[i & (-i)])
      (T::siz[v] == 1) && (T::to[u] ^= 1 << v, ++lf[u]);
  DFS(1, 0);
  int ans = 0;
  for (int i = (1); i <= (n); ++i) ans = (ans + dp[i][rt]) % P;
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n - 1); ++i)
    scanf("%d%d", &u, &v), G.add(u, v), G.add(v, u);
  scanf("%d", &m);
  for (int i = (1); i <= (m - 1); ++i)
    scanf("%d%d", &u, &v), --u, --v, H.add(u, v), H.add(v, u);
  if (m == 1) {
    printf("%d\n", n % P);
    return 0;
  }
  a[1] = 1;
  for (int i = (2); i <= ((1 << m) - 1); ++i)
    a[i] = a[i >> 1] + (i & 1), b[i] = b[i >> 1] + 1;
  fac[0] = 1;
  for (int i = (1); i <= (15); ++i) fac[i] = 1ll * fac[i - 1] * i % P;
  C[0][0] = 1;
  for (int i = (1); i <= (15); ++i) {
    C[i][0] = 1;
    for (int j = (1); j <= (i); ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
  }
  for (int i = (16); i <= (n); ++i) {
    C[i][0] = 1;
    for (int j = (1); j <= (15); ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
  }
  for (int i = (0); i <= (m - 1); ++i) {
    T::cls();
    dfs(i, -1);
    T::dfs(i);
    T::fxk = T::f[i];
    if (mp.count(T::fxk)) continue;
    mp.insert(T::fxk);
    ans = (ans + times(i)) % P;
  }
  printf("%d\n", ans);
  return 0;
}