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;
vector<int> T[12];
vector<int> S[1002];
int s;
int t;
long long int val[12];
map<int, vector<int> > mp;
int H[1002];
inline long long int dfs_T(int b, int pr = -1, int d = 0) {
  long long int r = 1;
  vector<int> vals;
  for (int go : T[b]) {
    if (go != pr) {
      int U = dfs_T(go, b, d + 1);
      vals.push_back(U);
      r *= (val[d] + U);
      if (r >= 1000000007) r %= 1000000007;
    }
  }
  sort(vals.begin(), vals.end());
  if (mp.count(r) == 0) {
    mp[r] = vals;
  }
  H[b] = r;
  return r;
}
unordered_map<int, int> dp[1002];
long long int tmp[1 << 12];
inline void dfs_S(int b, int pr) {
  vector<int> cd;
  for (int go : S[b]) {
    if (go != pr) {
      dfs_S(go, b);
      cd.emplace_back(go);
    }
  }
  for (auto &it : mp) {
    if (it.second.size() > cd.size()) continue;
    int sz = it.second.size();
    fill(tmp, tmp + (1 << sz), 0);
    tmp[0] = 1;
    for (int go : cd) {
      for (int i = (1 << sz) - 1; i >= 0; i--) {
        for (int j = 0; j < sz; j++) {
          if ((i >> j) & 1) continue;
          if (j && (((i >> (j - 1)) & 1) == 0) &&
              it.second[j] == it.second[j - 1])
            continue;
          tmp[i | (1 << j)] += tmp[i] * dp[go][it.second[j]];
          if (tmp[i | (1 << j)] >= 1000000007) {
            tmp[i | (1 << j)] %= 1000000007;
          }
        }
      }
    }
    dp[b][it.first] = tmp[(1 << sz) - 1];
  }
}
set<int> all;
int main() {
  cin >> s;
  for (int i = 1; i < s; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    S[a].push_back(b);
    S[b].push_back(a);
  }
  cin >> t;
  for (int i = 1; i < t; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    T[a].push_back(b);
    T[b].push_back(a);
  }
  mt19937 mt(time(NULL));
  uniform_int_distribution<int> uf(0, 1000000007 - 1);
  for (int i = 0; i < 12; i++) {
    val[i] = uf(mt);
  }
  for (int i = 0; i < t; i++) {
    dfs_T(i, -1, 0);
    all.insert(H[i]);
  }
  dfs_S(0, -1);
  int ans = 0;
  for (int i = 0; i < s; i++) {
    for (int k : all) {
      ans += dp[i][k];
      if (ans >= 1000000007) ans -= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}