Problem p03846 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1e+15;

// Residue Class Ring
namespace Xelmh_RCR {
class RCR {
  // Residue Class Ring
public:
  static const int64_t MOD = 1000000007;
  //        static const int64_t MOD = 998244353;
  int64_t val;
  RCR(int64_t l) : val{l % MOD} {}
  RCR() : val{0} {}

  RCR pow(RCR m) const {
    RCR rv{1};
    auto nmul = val;
    while (m.val > 0) {
      if ((m.val & 1) == 1) {
        rv.val *= nmul;
        rv.val %= MOD;
      }
      nmul *= nmul;
      nmul %= MOD;
      m.val /= 2;
    }
    return rv;
  }
  RCR inv() const { /* return Inverse */
    return this->pow(MOD - 2);
  }
  void operator+=(const RCR &a) {
    val += a.val;
    val %= MOD;
  }
  void operator-=(const RCR &a) {
    val -= a.val;
    val %= MOD;
  }
  void operator*=(const RCR &a) {
    val *= a.val;
    val %= MOD;
  }
  void operator/=(const RCR &a) {
    val *= a.inv().val;
    val %= MOD;
  }
};

// supported operator :: + - * / ^
RCR operator+(const RCR &a, const RCR &b) { return (a.val + b.val) % RCR::MOD; }
RCR operator-(const RCR &a, const RCR &b) {
  return (a.val - b.val + RCR::MOD) % RCR::MOD;
}
RCR operator*(const RCR &a, const RCR &b) { return (a.val * b.val) % RCR::MOD; }
RCR operator/(const RCR &a, const RCR &b) {
  return (a.val * b.inv().val) % RCR::MOD;
}
RCR operator^(const RCR &a, const RCR &b) { return a.pow(b); }

RCR operator==(const RCR &a, const RCR &b) { return a.val == b.val; }
RCR operator!=(const RCR &a, const RCR &b) { return a.val != b.val; }
// IO
std::ostream &operator<<(std::ostream &os, const RCR &m) { return os << m.val; }
std::istream &operator>>(std::istream &is, RCR &m) { return is >> m.val; }

RCR fact(ll N) {
  /* N -> N! */
  static vector<RCR> cal(1, RCR{1});

  if (cal.size() > N)
    return cal[N];
  else {
    RCR nm = fact(N - 1);
    assert(cal.size() == N);
    cal.push_back(nm * N);
    return cal[N];
  }
}

RCR choice(ll N, ll k) {
  /* N, k -> N C k */

  // N! / k!(N-k!)
  RCR ans = fact(N) / (fact(k) * fact(N - k));
  return ans;
}
} // namespace Xelmh_RCR
using namespace Xelmh_RCR;

void solve(istream &cin) {
  ll N;
  cin >> N;
  if (!cin)
    return;
  RCR ans = RCR(2).pow(N / 2);
  vector<int> v(N / 2, 0);
  for (int i = 0; i < N; ++i) {
    int t;
    cin >> t;
    if (t / 2 >= v.size()) {
      ans = 0;
    } else {
      v[t / 2]++;
      if (v[t / 2] > 2) {
        ans = 0;
      }
    }
  } // end i
  if (N % 2 == 1 and v[0] != 1) {
    ans = 0;
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef DEBUG
  cerr << "DEBUG MODE" << endl;
  string test_cases = "test.txt";
  auto fs = fstream(test_cases, fstream::in);
  while (fs) {
    solve(fs);
  }
#else
  solve(cin);
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1e+15;

// Residue Class Ring
namespace Xelmh_RCR {
class RCR {
  // Residue Class Ring
public:
  static const int64_t MOD = 1000000007;
  //        static const int64_t MOD = 998244353;
  int64_t val;
  RCR(int64_t l) : val{l % MOD} {}
  RCR() : val{0} {}

  RCR pow(RCR m) const {
    RCR rv{1};
    auto nmul = val;
    while (m.val > 0) {
      if ((m.val & 1) == 1) {
        rv.val *= nmul;
        rv.val %= MOD;
      }
      nmul *= nmul;
      nmul %= MOD;
      m.val /= 2;
    }
    return rv;
  }
  RCR inv() const { /* return Inverse */
    return this->pow(MOD - 2);
  }
  void operator+=(const RCR &a) {
    val += a.val;
    val %= MOD;
  }
  void operator-=(const RCR &a) {
    val -= a.val;
    val %= MOD;
  }
  void operator*=(const RCR &a) {
    val *= a.val;
    val %= MOD;
  }
  void operator/=(const RCR &a) {
    val *= a.inv().val;
    val %= MOD;
  }
};

// supported operator :: + - * / ^
RCR operator+(const RCR &a, const RCR &b) { return (a.val + b.val) % RCR::MOD; }
RCR operator-(const RCR &a, const RCR &b) {
  return (a.val - b.val + RCR::MOD) % RCR::MOD;
}
RCR operator*(const RCR &a, const RCR &b) { return (a.val * b.val) % RCR::MOD; }
RCR operator/(const RCR &a, const RCR &b) {
  return (a.val * b.inv().val) % RCR::MOD;
}
RCR operator^(const RCR &a, const RCR &b) { return a.pow(b); }

RCR operator==(const RCR &a, const RCR &b) { return a.val == b.val; }
RCR operator!=(const RCR &a, const RCR &b) { return a.val != b.val; }
// IO
std::ostream &operator<<(std::ostream &os, const RCR &m) { return os << m.val; }
std::istream &operator>>(std::istream &is, RCR &m) { return is >> m.val; }

RCR fact(ll N) {
  /* N -> N! */
  static vector<RCR> cal(1, RCR{1});

  if (cal.size() > N)
    return cal[N];
  else {
    RCR nm = fact(N - 1);
    assert(cal.size() == N);
    cal.push_back(nm * N);
    return cal[N];
  }
}

RCR choice(ll N, ll k) {
  /* N, k -> N C k */

  // N! / k!(N-k!)
  RCR ans = fact(N) / (fact(k) * fact(N - k));
  return ans;
}
} // namespace Xelmh_RCR
using namespace Xelmh_RCR;

void solve(istream &cin) {
  ll N;
  cin >> N;
  if (!cin)
    return;
  RCR ans = RCR(2).pow(N / 2);
  vector<int> v((N + 1) / 2, 0);
  for (int i = 0; i < N; ++i) {
    int t;
    cin >> t;
    if (t / 2 >= v.size()) {
      ans = 0;
    } else {
      v[t / 2]++;
      if (v[t / 2] > 2) {
        ans = 0;
      }
    }
  } // end i
  if (N % 2 == 1 and v[0] != 1) {
    ans = 0;
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef DEBUG
  cerr << "DEBUG MODE" << endl;
  string test_cases = "test.txt";
  auto fs = fstream(test_cases, fstream::in);
  while (fs) {
    solve(fs);
  }
#else
  solve(cin);
#endif
  return 0;
}