Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
#define MOD 1000000007

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(long long p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator int() const { return x; } // added by QCFium
  ModInt operator=(const int p) {
    x = p;
    return ModInt(*this);
  } // added by QCFium
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};
typedef ModInt<MOD> mint;

struct MComb {
  std::vector<mint> fact;
  std::vector<mint> inversed;
  MComb(int n) { // O(n+log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inversed = std::vector<mint>(n + 1);
    inversed[n] = fact[n] ^ (MOD - 2);
    for (int i = n - 1; i >= 0; i--)
      inversed[i] = inversed[i + 1] * mint(i + 1);
  }
  mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
  mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
  mint nhr(int n, int r) {
    assert(n + r - 1 < (int)fact.size());
    return ncr(n + r - 1, r);
  }
};

int main() {
  int n = ri();
  std::vector<int> hen[n];
  for (int i = 1; i < n; i++) {
    int a = ri() - 1;
    int b = ri() - 1;
    hen[a].push_back(b);
    hen[b].push_back(a);
  }
  int parent[n];
  parent[0] = -1;
  std::queue<int> que;
  que.push(0);
  while (que.size()) {
    int i = que.front();
    que.pop();
    for (auto j : hen[i])
      if (j != parent[i])
        parent[j] = i, que.push(j);
  }

  int ok[n];
  memset(ok, 0, sizeof(ok));
  int m = ri();
  for (int i = 0; i < m; i++) {
    int x = ri() - 1;
    int y = ri() - 1;
    std::vector<int> r0, r1;
    while (x != -1)
      r0.push_back(x), x = parent[x];
    while (y != -1)
      r1.push_back(y), y = parent[y];
    while (r0.size() && r1.size() && r0.back() == r1.back())
      r0.pop_back(), r1.pop_back();
    for (auto j : r0)
      ok[j] |= 1 << i;
    for (auto j : r1)
      ok[j] |= 1 << i;
  }
  int64_t dp[n + 1][1 << m];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1 << m; j++) {
      // use
      if (i)
        dp[i + 1][j | ok[i]] += dp[i][j];
      // no use
      dp[i + 1][j] += dp[i][j];
    }
  }
  std::cout << dp[n][(1 << m) - 1] << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
#define MOD 1000000007

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(long long p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator int() const { return x; } // added by QCFium
  ModInt operator=(const int p) {
    x = p;
    return ModInt(*this);
  } // added by QCFium
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};
typedef ModInt<MOD> mint;

struct MComb {
  std::vector<mint> fact;
  std::vector<mint> inversed;
  MComb(int n) { // O(n+log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inversed = std::vector<mint>(n + 1);
    inversed[n] = fact[n] ^ (MOD - 2);
    for (int i = n - 1; i >= 0; i--)
      inversed[i] = inversed[i + 1] * mint(i + 1);
  }
  mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
  mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
  mint nhr(int n, int r) {
    assert(n + r - 1 < (int)fact.size());
    return ncr(n + r - 1, r);
  }
};

int main() {
  int n = ri();
  std::vector<int> hen[n];
  for (int i = 1; i < n; i++) {
    int a = ri() - 1;
    int b = ri() - 1;
    hen[a].push_back(b);
    hen[b].push_back(a);
  }
  int parent[n];
  parent[0] = -1;
  std::queue<int> que;
  que.push(0);
  while (que.size()) {
    int i = que.front();
    que.pop();
    for (auto j : hen[i])
      if (j != parent[i])
        parent[j] = i, que.push(j);
  }

  int ok[n];
  memset(ok, 0, sizeof(ok));
  int m = ri();
  for (int i = 0; i < m; i++) {
    int x = ri() - 1;
    int y = ri() - 1;
    std::vector<int> r0, r1;
    while (x != -1)
      r0.push_back(x), x = parent[x];
    while (y != -1)
      r1.push_back(y), y = parent[y];
    while (r0.size() && r1.size() && r0.back() == r1.back())
      r0.pop_back(), r1.pop_back();
    for (auto j : r0)
      ok[j] |= 1 << i;
    for (auto j : r1)
      ok[j] |= 1 << i;
  }
  std::vector<std::vector<int64_t>> dp(n + 1, std::vector<int64_t>(1 << m));
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1 << m; j++) {
      // use
      if (i)
        dp[i + 1][j | ok[i]] += dp[i][j];
      // no use
      dp[i + 1][j] += dp[i][j];
    }
  }
  std::cout << dp[n][(1 << m) - 1] << std::endl;

  return 0;
}