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 mod = 1e9 + 7, N = 1000, M = 12;
inline void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
inline void sub(int &x, int y) {
  x -= y;
  if (x < 0) x += mod;
}
struct Tree : vector<vector<int>> {
  void AddEdge(int x, int y) {
    this->at(x).push_back(y), this->at(y).push_back(x);
  }
};
void Del(vector<int> &vec, int element) {
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    while (*it == element) {
      it = vec.erase(it);
      if (it == vec.end()) return;
    }
  }
}
Tree getroot(Tree x, int rt) {
  queue<int> que;
  que.push(rt);
  while (que.size()) {
    int cur = que.front();
    que.pop();
    for (auto nxt : x[cur]) {
      Del(x[nxt], cur);
      que.push(nxt);
    }
  }
  return x;
}
int Log2[1 << M];
struct TreeMatcher {
  vector<int> dp[N][M];
  int dfs(Tree &S, Tree &T, int s, int t) {
    vector<int> &cur = dp[s][t];
    if (!cur.empty()) return cur.back();
    cur.resize(1 << T[t].size());
    cur[0] = 1;
    for (auto nxt : S[s]) {
      for (int i = (int)cur.size() - 1; ~i; --i)
        for (int j = i; j; j ^= (j & -j)) {
          int node = Log2[j & -j];
          if (cur[i ^ (1 << node)])
            add(cur[i],
                1LL * cur[i ^ (1 << node)] * dfs(S, T, nxt, T[t][node]) % mod);
        }
    }
    return cur.back();
  }
  int match(Tree &S, Tree &T) {
    if (S.size() < T.size()) return 0;
    if (T.size() <= 1) return S.size();
    auto clr = [&] {
      for (int i = 0; i < S.size(); ++i)
        for (int j = 0; j < T.size(); ++j) dp[i][j].clear();
    };
    int ans = 0;
    Tree s = getroot(S, 0);
    for (int j = 0; j < T.size(); ++j) {
      Tree t = getroot(T, j);
      clr();
      for (int i = 0; i < S.size(); ++i) add(ans, dfs(s, t, i, j));
    }
    return ans;
  }
};
inline int Inv(int x) {
  return x <= 1 ? x : 1LL * (mod - mod / x) * Inv(mod % x) % mod;
}
Tree S, T;
TreeMatcher matcher;
int main() {
  auto Input = [&](Tree &tree) {
    int n;
    scanf("%d", &n);
    tree.resize(n);
    for (int i = 1, x, y; i < n; ++i) {
      scanf("%d %d", &x, &y);
      tree.AddEdge(x - 1, y - 1);
    }
  };
  Input(S), Input(T);
  *Log2 = -1;
  for (int i = 1; i < (1 << M); ++i) Log2[i] = Log2[i >> 1] + 1;
  printf("%lld\n", 1LL * matcher.match(S, T) * Inv(matcher.match(T, T)) % mod);
  return 0;
}