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>
int CH, NEG;
template <typename TP>
inline void read(TP &ret) {
  ret = NEG = 0;
  while (CH = getchar(), CH < '!')
    ;
  if (CH == '-') NEG = true, CH = getchar();
  while (ret = ret * 10 + CH - '0', CH = getchar(), CH > '!')
    ;
  if (NEG) ret = -ret;
}
using namespace std;
int n, m, ANS;
int now[(1 << 12LL) + 5], nxt[(1 << 12LL) + 5];
vector<int> G[1000LL + 5], g[12LL + 5];
map<int, vector<int> > STA;
map<int, int> ans[1000LL + 5];
inline int blen(int x) { return 32 - __builtin_clz(x); }
inline int combine(int x, int y) { return x << blen(y) | y; }
inline void P(int x) {
  for (int i = 30; ~i; i--) {
    printf("%d", int(bool(x & (1 << i))));
  }
  puts("");
}
int dfs0(int u, int fa = 0) {
  int h = 1;
  vector<int> H;
  for (auto v : g[u])
    if (v != fa) H.push_back(dfs0(v, u));
  sort(H.begin(), H.end());
  for (auto s : H) h = combine(h, s);
  h <<= 1;
  if (!STA.count(h)) STA[h] = H;
  return h;
}
void dfs(int u, int fa = 0) {
  vector<int> chd;
  for (auto v : G[u])
    if (v != fa) chd.push_back(v), dfs(v, u);
  for (const auto &sta : STA) {
    auto &H = sta.second;
    int h = sta.first, N = H.size();
    memset(now, 0, sizeof(int) * (1 << N));
    now[0] = 1;
    for (auto ch : chd) {
      memcpy(nxt, now, sizeof(int) * (1 << N));
      for (int i = 0; i < N; i++) {
        int num = ans[ch][H[i]];
        if (!num) continue;
        for (int j = (1 << N) - 1; j >= 0; j--)
          if (now[j] && !(j >> i & 1) &&
              !(i && H[i] == H[i - 1] && !(j >> (i - 1) & 1)))
            (nxt[j | (1 << i)] += (long long)now[j] * num % 1000000007LL) %=
                1000000007LL;
      }
      memcpy(now, nxt, sizeof(int) * (1 << N));
    }
    if (now[(1 << N) - 1]) {
      ans[u][h] = now[(1 << N) - 1];
      if (blen(h) == (m << 1)) (ANS += now[(1 << N) - 1]) %= 1000000007LL;
    }
  }
}
int main() {
  int i, u, v;
  read(n);
  for (i = (1); i < (n); i++)
    read(u), read(v), G[u].push_back(v), G[v].push_back(u);
  read(m);
  for (i = (1); i < (m); i++)
    read(u), read(v), g[u].push_back(v), g[v].push_back(u);
  for (i = (1); i <= (m); i++) dfs0(i);
  dfs(1);
  (ANS += 1000000007LL) %= 1000000007LL;
  printf("%d\n", ANS);
END:
  getchar(), getchar();
  return 0;
}