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;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int maxn = 1000, maxm = 12;
int n, m;
vector<int> adj[maxn + 5], adjf[maxm + 5];
map<int, vector<int> > way;
inline int blen(int first) { return 32 - __builtin_clz(first); }
inline int combine(int first, int second) {
  return first << blen(second) | second;
}
int dfsf(int first, int f = -1) {
  vector<int> s;
  for (auto second : adjf[first])
    if (second != f) s.push_back(dfsf(second, first));
  sort((s).begin(), (s).end());
  int ret = 1;
  for (auto u : s) ret = combine(ret, u);
  ret <<= 1;
  if (!way.count(ret)) way[ret] = s;
  return ret;
}
int ans;
map<int, int> dp[maxn + 5];
void dfs(int first, int f = -1) {
  vector<int> chd;
  for (auto second : adj[first])
    if (second != f) {
      chd.push_back(second);
      dfs(second, first);
    }
  for (const auto &u : way) {
    auto &v = u.second;
    int N = (int((u.second).size()));
    static int pd[(1 << maxm) + 5];
    memset(pd, 0, sizeof(pd[0]) * (1 << N));
    pd[0] = 1;
    for (auto second : chd) {
      static int nxt[(1 << maxm) + 5];
      memcpy(nxt, pd, sizeof(nxt[0]) * (1 << N));
      for (int i = (0), i_end_ = (N); i < i_end_; ++i) {
        int num = dp[second][v[i]];
        if (!num) continue;
        for (int j = (1 << N) - 1; j >= 0; --j)
          if (pd[j] && !(j >> i & 1) &&
              !(i && v[i] == v[i - 1] && !(j >> (i - 1) & 1)))
            (nxt[j | (1 << i)] += (long long)pd[j] * num % Mod) %= Mod;
      }
      memcpy(pd, nxt, sizeof(pd[0]) * (1 << N));
    }
    if (pd[(1 << N) - 1]) {
      dp[first][u.first] = pd[(1 << N) - 1];
      if (blen(u.first) == (m << 1)) (ans += pd[(1 << N) - 1]) %= Mod;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (0), i_end_ = (n - 1); i < i_end_; ++i) {
    int u, v;
    scanf("%d%d", &u, &v), --u, --v;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  scanf("%d", &m);
  for (int i = (0), i_end_ = (m - 1); i < i_end_; ++i) {
    int u, v;
    scanf("%d%d", &u, &v), --u, --v;
    adjf[u].push_back(v), adjf[v].push_back(u);
  }
  for (int i = (0), i_end_ = (m); i < i_end_; ++i) dfsf(i);
  ans = 0;
  dfs(0);
  (ans += Mod) %= Mod;
  printf("%d\n", ans);
  return 0;
}