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 read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
struct modint {
  int x;
  modint(int o = 0) { x = o; }
  modint &operator=(int o) { return x = o, *this; }
  modint &operator+=(modint o) {
    return x = x + o.x >= 1000000007 ? x + o.x - 1000000007 : x + o.x, *this;
  }
  modint &operator-=(modint o) {
    return x = x - o.x < 0 ? x - o.x + 1000000007 : x - o.x, *this;
  }
  modint &operator*=(modint o) { return x = 1ll * x * o.x % 1000000007, *this; }
  modint &operator^=(int b) {
    modint a = *this, c = 1;
    for (; b; b >>= 1, a *= a)
      if (b & 1) c *= a;
    return x = c.x, *this;
  }
  modint &operator/=(modint o) { return *this *= o ^= 1000000007 - 2; }
  modint &operator+=(int o) {
    return x = x + o >= 1000000007 ? x + o - 1000000007 : x + o, *this;
  }
  modint &operator-=(int o) {
    return x = x - o < 0 ? x - o + 1000000007 : x - o, *this;
  }
  modint &operator*=(int o) { return x = 1ll * x * o % 1000000007, *this; }
  modint &operator/=(int o) { return *this *= ((modint(o)) ^= 1000000007 - 2); }
  template <class I>
  friend modint operator+(modint a, I b) {
    return a += b;
  }
  template <class I>
  friend modint operator-(modint a, I b) {
    return a -= b;
  }
  template <class I>
  friend modint operator*(modint a, I b) {
    return a *= b;
  }
  template <class I>
  friend modint operator/(modint a, I b) {
    return a /= b;
  }
  friend modint operator^(modint a, int b) { return a ^= b; }
  friend bool operator==(modint a, int b) { return a.x == b; }
  friend bool operator!=(modint a, int b) { return a.x != b; }
  bool operator!() { return !x; }
  modint operator-() { return x ? 1000000007 - x : 0; }
};
struct tree {
  int n;
  vector<int> e[1005];
  void input() {
    n = read();
    for (register int i = (2); i <= (n); ++i) {
      int u = read() - 1, v = read() - 1;
      e[u].push_back(v), e[v].push_back(u);
    }
  }
} s, t;
vector<modint> f[1005][12];
int lg[1 << 12 | 5];
inline void del(vector<int> &v, int x) {
  for (int i = 0; i < v.size(); ++i)
    if (v[i] == x) {
      v.erase(v.begin() + i);
      return;
    }
}
tree root(tree s, int rt) {
  queue<int> q;
  q.push(rt);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : s.e[u]) {
      del(s.e[v], u);
      q.push(v);
    }
  }
  return s;
}
modint dfs(tree &s, tree &t, int u, int v) {
  if (f[u][v].size()) return f[u][v].back();
  vector<modint> &res = f[u][v];
  res.resize(1 << t.e[v].size()), res[0] = 1;
  for (auto x : s.e[u]) {
    for (register int i = ((int)res.size() - 1); i >= (0); --i)
      for (int j = i; j; j ^= (j & -j)) {
        int y = lg[j & -j];
        if (res[i ^ (1 << y)].x)
          res[i] += res[i ^ (1 << y)] * dfs(s, t, x, t.e[v][y]);
      }
  }
  return res.back();
}
modint treematch(tree s, tree t) {
  if (s.n < t.n) return 0;
  if (t.n <= 1) return s.n;
  s = root(s, 0);
  modint res = 0;
  for (register int j = (0); j <= (t.n - 1); ++j) {
    tree tmp = root(t, j);
    for (register int i = (0); i <= (s.n - 1); ++i)
      for (register int k = (0); k <= (t.n - 1); ++k) f[i][k].clear();
    for (register int i = (0); i <= (s.n - 1); ++i) res += dfs(s, tmp, i, j);
  }
  return res;
}
int main() {
  s.input(), t.input();
  lg[0] = -1;
  for (register int i = (1); i <= ((1 << t.n) - 1); ++i) lg[i] = lg[i >> 1] + 1;
  modint res1 = treematch(s, t);
  modint res2 = treematch(t, t);
  cout << (res1 / res2).x;
  return 0;
}