#include <bits/stdc++.h>
using namespace std;
using vint = vector<long long>;
using pint = pair<long long, long long>;
using vpint = vector<pint>;
template <typename A, typename B>
inline void chmin(A& a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A& a, B b) {
  if (a < b) a = b;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
  ost << "{" << p.first << "," << p.second << "}";
  return ost;
}
template <class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
  ost << "{";
  for (long long i = 0; i < v.size(); i++) {
    if (i) ost << ",";
    ost << v[i];
  }
  ost << "}";
  return ost;
}
inline long long topbit(unsigned long long x) {
  return x ? 63 - __builtin_clzll(x) : -1;
}
inline long long popcount(unsigned long long x) {
  return __builtin_popcountll(x);
}
inline long long parity(unsigned long long x) { return __builtin_parity(x); }
template <uint32_t mod>
struct ModInt {
  uint32_t a;
  ModInt& s(uint32_t vv) {
    a = vv < mod ? vv : vv - mod;
    return *this;
  }
  ModInt(int64_t x = 0) { s(x % mod + mod); }
  ModInt& operator+=(const ModInt& x) { return s(a + x.a); }
  ModInt& operator-=(const ModInt& x) { return s(a + mod - x.a); }
  ModInt& operator*=(const ModInt& x) {
    a = uint64_t(a) * x.a % mod;
    return *this;
  }
  ModInt& operator/=(const ModInt& x) {
    *this *= x.inv();
    return *this;
  }
  ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }
  bool operator==(const ModInt& x) const { return a == x.a; }
  bool operator!=(const ModInt& x) const { return a != x.a; }
  bool operator<(const ModInt& x) const { return a < x.a; }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt pow(int64_t n) const {
    ModInt res(1), x(*this);
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  ModInt inv() const { return pow(mod - 2); }
};
template <uint32_t mod>
istream& operator>>(istream& in, ModInt<mod>& a) {
  return (in >> a.a);
}
template <uint32_t mod>
ostream& operator<<(ostream& out, const ModInt<mod>& a) {
  return (out << a.a);
}
using mint = ModInt<1000000007>;
long long N;
long long M;
vint S[1111], T[22];
long long id[22][22];
pint mem[33];
mint dp[1111][33];
mint dp2[1 << 12];
mint ans;
void dfs(long long v, long long p) {
  for (auto u : S[v]) {
    if (u == p) continue;
    dfs(u, v);
  }
  for (long long i = 0; i < (M); i++) {
    long long cs = T[i].size();
    for (long long j = 0; j < (1 << cs); j++) dp2[j] = 0;
    dp2[0] = 1;
    for (auto u : S[v]) {
      if (u == p) continue;
      for (long long j = (1 << cs) - 1; j >= 0; j--) {
        for (long long k = 0; k < (cs); k++)
          if (!(j >> k & 1))
            dp2[j | (1 << k)] += dp2[j] * dp[u][id[i][T[i][k]]];
      }
    }
    ans += dp2[(1 << cs) - 1];
    for (long long k = 0; k < (cs); k++) {
      dp[v][id[T[i][k]][i]] = dp2[((1 << cs) - 1) ^ (1 << k)];
    }
  }
}
signed main() {
  cin >> N;
  for (long long i = 0; i < (N - 1); i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    S[a].push_back(b);
    S[b].push_back(a);
  }
  cin >> M;
  for (long long i = 0; i < (M - 1); i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    T[a].push_back(b);
    T[b].push_back(a);
    id[a][b] = i * 2;
    mem[i * 2] = pint(a, b);
    id[b][a] = i * 2 + 1;
    mem[i * 2 + 1] = pint(b, a);
  }
  dfs(0, -1);
  mint tmp = ans;
  ans = 0;
  for (long long i = 0; i < (M); i++) {
    S[i] = T[i];
  }
  for (long long i = 0; i < (M); i++)
    for (long long j = 0; j < (33); j++) dp[i][j] = 0;
  dfs(0, -1);
  ans = tmp / ans;
  cout << ans << endl;
  return 0;
}
