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;
inline int Get() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const long long mod = 1e9 + 7;
const long long Mod = 998244353;
int n, m;
long long f[2005][15];
struct road {
  int to, next;
} s[2005 << 1];
int h[2005], cnt;
void add(int i, int j) {
  s[++cnt] = (road){j, h[i]};
  h[i] = cnt;
}
vector<int> e[2005];
long long fac[2005], ifac[2005];
long long ksm(long long t, long long x) {
  long long ans = 1;
  for (; x; x >>= 1, t = t * t % mod)
    if (x & 1) ans = ans * t % mod;
  return ans;
}
long long Hash[2005];
long long Hash2[2005];
const long long p = 666233, p2 = 134341;
unsigned long long p3 = 233333;
int size[2005];
bool cmp(int a, int b) {
  if (size[a] != size[b]) return size[a] < size[b];
  if (Hash[a] != Hash[b]) return Hash[a] < Hash[b];
  return Hash2[a] < Hash2[b];
}
long long t[2005];
vector<int> sn[2005];
int dep[2005], mxdep[2005];
void dfs1(int v, int fr) {
  size[v] = 1;
  mxdep[v] = dep[v];
  for (int i = 0; i < e[v].size(); i++) {
    int to = e[v][i];
    if (to == fr) continue;
    sn[v].push_back(to);
    dep[to] = dep[v] + 1;
    dfs1(to, v);
    mxdep[v] = max(mxdep[v], mxdep[to]);
    size[v] += size[to];
  }
  sort(sn[v].begin(), sn[v].end(), cmp);
  t[v] = 1;
  for (int i = 0; i < sn[v].size(); i++) {
    int j = i;
    while (j + 1 < sn[v].size() && size[sn[v][i]] == size[sn[v][j + 1]] &&
           Hash[sn[v][i]] == Hash[sn[v][j + 1]] &&
           Hash2[sn[v][j]] == Hash2[sn[v][j + 1]])
      j++;
    t[v] = t[v] * ifac[j - i + 1] % mod;
    i = j;
  }
  Hash[v] = (Hash[v] * p + size[v]) % mod;
  Hash2[v] = (Hash2[v] * p2 + size[v]) % Mod;
  for (int i = 0; i < sn[v].size(); i++) {
    Hash[v] = (Hash[v] * p + Hash[sn[v][i]]) % mod;
    Hash2[v] = (Hash2[v] * p2 + Hash2[sn[v][i]]) % Mod;
  }
}
int st[2005], top;
int cal(int v, int u) {
  if (!top) return 0;
  static long long ans[1 << 13], g[1 << 13];
  int num = sn[u].size();
  for (int i = 0; i < 1 << num; i++) ans[i] = 0;
  ans[0] = 1;
  for (int i = 1; i <= top; i++) {
    memcpy(g, ans, sizeof(g));
    int now = st[i];
    for (int j = 0; j < num; j++) {
      if (!f[now][sn[u][j]]) continue;
      int t = 1 << j;
      for (int sta = 0; sta < 1 << num; sta++) {
        if (!(sta & t))
          g[sta | t] = (g[sta | t] + ans[sta] * f[now][sn[u][j]]) % mod;
      }
    }
    memcpy(ans, g, sizeof(g));
  }
  return ans[(1 << num) - 1] * t[u] % mod;
}
long long ans;
int rt;
void solve(int v, int fr) {
  for (int i = 1; i <= m; i++)
    if (sn[i].size() == 0) f[v][i] = 1;
  for (int i = h[v]; i; i = s[i].next) {
    int to = s[i].to;
    if (to == fr) continue;
    solve(to, v);
  }
  top = 0;
  for (int i = h[v]; i; i = s[i].next) {
    if (s[i].to == fr) continue;
    st[++top] = s[i].to;
  }
  for (int i = 1; i <= m; i++) {
    if (sn[i].size()) {
      f[v][i] = cal(v, i);
    }
  }
  (ans += f[v][rt]) %= mod;
}
map<pair<int, int>, int> vis;
int work() {
  for (int i = 1; i <= m; i++) sn[i].clear();
  for (int i = 1; i <= m; i++) Hash[i] = Hash2[i] = 0;
  dep[rt] = 1;
  dfs1(rt, 0);
  if (vis.find(make_pair(Hash[rt], Hash2[rt])) != vis.end()) return 0;
  vis[make_pair(Hash[rt], Hash2[rt])] = 1;
  memset(f, 0, sizeof(f));
  ans = 0;
  solve(1, 0);
  return ans;
}
int main() {
  n = Get();
  int a, b;
  for (int i = 1; i < n; i++) {
    a = Get(), b = Get();
    add(a, b), add(b, a);
  }
  m = Get();
  fac[0] = 1;
  for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % mod;
  ifac[m] = ksm(fac[m], mod - 2);
  for (int i = m - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;
  for (int i = 1; i < m; i++) {
    a = Get(), b = Get();
    e[a].push_back(b);
    e[b].push_back(a);
  }
  long long tot = 0;
  for (rt = m; rt >= 1; rt--) (tot += work()) %= mod;
  cout << tot << "\n";
  return 0;
}