Problem p03025 (C++)

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

namespace MMNMM {
template <class SemiGroup, class size_type>
constexpr SemiGroup pow(const SemiGroup &a, size_type n,
                        const SemiGroup &b = SemiGroup::e()) {
  SemiGroup ret = b, p = a;
  while (n) {
    if (n & 1)
      ret *= p;
    p *= p;
    n >>= 1;
  }
  return ret;
}
template <class UnsignedInteger = unsigned long,
          UnsignedInteger Mod = 1000000007>
class modint {
  static_assert(Mod & 1, "mod mast be odd");

  using value_type = UnsignedInteger;
  using const_value = const value_type;
  using reference_type = value_type &;
  using const_reference = const_value &;

  static constexpr size_t bit_length = 32;
  static_assert(Mod < static_cast<UnsignedInteger>(1) << bit_length - 2,
                "mod is too big");
  static constexpr value_type mask =
      ~(~static_cast<UnsignedInteger>(0) << bit_length);

  static constexpr value_type f(value_type n) {
    value_type ret = 1;
    for (auto _ = 0; _ < 5; ++_)
      ret = ret * (2 - ret * n);
    return -ret & mask;
  }

  static constexpr const_value minv{f(Mod)};
  static constexpr const_value r2{-Mod % Mod};
  static constexpr const_value r{
      (static_cast<UnsignedInteger>(1) << bit_length) % Mod};

  static constexpr value_type reduce(const_value v) {
    return (v + Mod * ((v * minv) & mask)) >> bit_length;
  }

  value_type val;
  static constexpr value_type upperbound = 2 * Mod;

public:
  static constexpr modint e() { return modint(1); }

  static constexpr value_type get_minv() noexcept { return minv; }

  static constexpr value_type get_r2() noexcept { return r2; }

  static constexpr value_type get_mod() noexcept { return Mod; }

  constexpr value_type get_value() const noexcept {
    return val == Mod ? 0 : reduce(val);
  }

  constexpr value_type get_raw() const noexcept { return val; }

  constexpr modint() : val(0) {}

  template <class T> constexpr modint(const T &_v = 0) : val(reduce(_v * r2)) {}

  template <class UnsignedInteger2, UnsignedInteger2 Mod2>
  constexpr modint(const modint<UnsignedInteger2, Mod2> &_v)
      : val(reduce(_v.get_value() * r2)) {}

  template <class UnsignedInteger2>
  constexpr modint(const modint<UnsignedInteger2, Mod> &_v)
      : val(_v.get_raw()) {}

  constexpr modint(const modint &_v) = default;

  constexpr const modint operator++(int) noexcept {
    auto t = *this;
    val += r;
    val -= (val >= upperbound) * upperbound;
    return t;
  }

  constexpr const modint operator--(int) noexcept {
    auto t = *this;
    val += (val < r) * upperbound;
    val -= r;
    return t;
  }

  constexpr modint &operator++() noexcept {
    val += r;
    val -= (val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator--() noexcept {
    val += (val < r) * upperbound;
    val -= r;
    return *this;
  }

  constexpr modint operator+() const noexcept { return *this; }

  constexpr modint operator-() const noexcept {
    auto t = modint(upperbound - this->get_value());
    return t;
  }

  explicit operator value_type() const noexcept { return this->val; }

  constexpr modint &operator=(const modint &rhs) &noexcept {
    this->val = rhs.get_raw();
    return *this;
  }

  constexpr modint &operator+=(const modint &rhs) noexcept {
    this->val += rhs.get_raw();
    this->val -= (this->val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator-=(const modint &rhs) noexcept {
    this->val += upperbound - rhs.get_raw();
    this->val -= (this->val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator*=(const modint &rhs) noexcept {
    this->val *= rhs.get_raw();
    this->val = reduce(this->val);
    return *this;
  }

  constexpr modint &operator/=(const modint &rhs) noexcept {
    *this = pow(rhs, Mod - 2, *this);
    return *this;
  }

  constexpr modint &operator|=(const modint &rhs) noexcept {
    this->val += rhs.get_raw();
    return *this;
  }

  constexpr void regularize() noexcept { this->val %= upperbound; }

  constexpr modint operator+(const modint &rhs) noexcept {
    auto t = *this;
    t += rhs;
    return t;
  }

  constexpr modint operator-(const modint &rhs) noexcept {
    auto t = *this;
    t -= rhs;
    return t;
  }

  constexpr modint operator*(const modint &rhs) noexcept {
    auto t = *this;
    t *= rhs;
    return t;
  }

  constexpr modint operator/(const modint &rhs) noexcept {
    auto t = *this;
    t /= rhs;
    return t;
  }
};

template <class UnsignedInteger, UnsignedInteger Mod>
std::ostream &operator<<(std::ostream &os,
                         const modint<UnsignedInteger, Mod> &mint) {
  os << mint.get_value();
  return os;
}
template <class UnsignedInteger, UnsignedInteger Mod>
std::istream &operator>>(std::istream &is, modint<UnsignedInteger, Mod> &mint) {
  UnsignedInteger t;
  is >> t;
  mint = modint<UnsignedInteger, Mod>(t);
  return is;
}
} // namespace MMNMM

int main() {
  using namespace std;
  constexpr unsigned long MOD = 1000000007;
  using modint = MMNMM::modint<>;
  modint N, A, B, C;
  cin >> N >> A >> B >> C;
  const unsigned long Nn = N.get_value();
  A /= 100;
  B /= 100;
  C /= 100;
  C = -C + 1;
  C /= C * C;
  A *= C;
  B *= C;
  vector<modint> ifr(Nn, 1);
  for (unsigned long i = 2; i != Nn; ++i)
    ifr[i] = ifr[MOD % i] * (MOD - MOD / i);
  modint ansA{0}, ansB{0}, Ap{1}, Bp{1}, a = C * N, bin = 1;
  for (unsigned long i = 0; i != Nn; ++i) {
    ansA |= a * bin * Ap;
    ansB |= a * bin * Bp;
    a += C;
    Ap *= A;
    Bp *= B;
    bin *= (N + i) * ifr[i + 1];
  }
  ansA.regularize();
  ansB.regularize();
  cout << ansA * Bp + ansB * Ap << endl;
  return 0;
}

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

namespace MMNMM {
template <class SemiGroup, class size_type>
constexpr SemiGroup pow(const SemiGroup &a, size_type n,
                        const SemiGroup &b = SemiGroup::e()) {
  SemiGroup ret = b, p = a;
  while (n) {
    if (n & 1)
      ret *= p;
    p *= p;
    n >>= 1;
  }
  return ret;
}
template <class UnsignedInteger = unsigned long,
          UnsignedInteger Mod = 1000000007>
class modint {
  static_assert(Mod & 1, "mod mast be odd");

  using value_type = UnsignedInteger;
  using const_value = const value_type;
  using reference_type = value_type &;
  using const_reference = const_value &;

  static constexpr size_t bit_length = 32;
  static_assert(Mod < static_cast<UnsignedInteger>(1) << bit_length - 2,
                "mod is too big");
  static constexpr value_type mask =
      ~(~static_cast<UnsignedInteger>(0) << bit_length);

  static constexpr value_type f(value_type n) {
    value_type ret = 1;
    for (auto _ = 0; _ < 5; ++_)
      ret = ret * (2 - ret * n);
    return -ret & mask;
  }

  static constexpr const_value minv{f(Mod)};
  static constexpr const_value r2{-Mod % Mod};
  static constexpr const_value r{
      (static_cast<UnsignedInteger>(1) << bit_length) % Mod};

  static constexpr value_type reduce(const_value v) {
    return (v + Mod * ((v * minv) & mask)) >> bit_length;
  }

  value_type val;
  static constexpr value_type upperbound = 2 * Mod;

public:
  static constexpr modint e() { return modint(1); }

  static constexpr value_type get_minv() noexcept { return minv; }

  static constexpr value_type get_r2() noexcept { return r2; }

  static constexpr value_type get_mod() noexcept { return Mod; }

  constexpr value_type get_value() const noexcept {
    return val == Mod ? 0 : reduce(val);
  }

  constexpr value_type get_raw() const noexcept { return val; }

  constexpr modint() : val(0) {}

  template <class T> constexpr modint(const T &_v = 0) : val(reduce(_v * r2)) {}

  template <class UnsignedInteger2, UnsignedInteger2 Mod2>
  constexpr modint(const modint<UnsignedInteger2, Mod2> &_v)
      : val(reduce(_v.get_value() * r2)) {}

  template <class UnsignedInteger2>
  constexpr modint(const modint<UnsignedInteger2, Mod> &_v)
      : val(_v.get_raw()) {}

  constexpr modint(const modint &_v) = default;

  constexpr const modint operator++(int) noexcept {
    auto t = *this;
    val += r;
    val -= (val >= upperbound) * upperbound;
    return t;
  }

  constexpr const modint operator--(int) noexcept {
    auto t = *this;
    val += (val < r) * upperbound;
    val -= r;
    return t;
  }

  constexpr modint &operator++() noexcept {
    val += r;
    val -= (val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator--() noexcept {
    val += (val < r) * upperbound;
    val -= r;
    return *this;
  }

  constexpr modint operator+() const noexcept { return *this; }

  constexpr modint operator-() const noexcept {
    auto t = modint(upperbound - this->get_value());
    return t;
  }

  explicit operator value_type() const noexcept { return this->val; }

  constexpr modint &operator=(const modint &rhs) &noexcept {
    this->val = rhs.get_raw();
    return *this;
  }

  constexpr modint &operator+=(const modint &rhs) noexcept {
    this->val += rhs.get_raw();
    this->val -= (this->val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator-=(const modint &rhs) noexcept {
    this->val += upperbound - rhs.get_raw();
    this->val -= (this->val >= upperbound) * upperbound;
    return *this;
  }

  constexpr modint &operator*=(const modint &rhs) noexcept {
    this->val *= rhs.get_raw();
    this->val = reduce(this->val);
    return *this;
  }

  constexpr modint &operator/=(const modint &rhs) noexcept {
    *this = pow(rhs, Mod - 2, *this);
    return *this;
  }

  constexpr modint &operator|=(const modint &rhs) noexcept {
    this->val += rhs.get_raw();
    return *this;
  }

  constexpr void regularize() noexcept { this->val %= upperbound; }

  constexpr modint operator+(const modint &rhs) noexcept {
    auto t = *this;
    t += rhs;
    return t;
  }

  constexpr modint operator-(const modint &rhs) noexcept {
    auto t = *this;
    t -= rhs;
    return t;
  }

  constexpr modint operator*(const modint &rhs) noexcept {
    auto t = *this;
    t *= rhs;
    return t;
  }

  constexpr modint operator/(const modint &rhs) noexcept {
    auto t = *this;
    t /= rhs;
    return t;
  }
};

template <class UnsignedInteger, UnsignedInteger Mod>
std::ostream &operator<<(std::ostream &os,
                         const modint<UnsignedInteger, Mod> &mint) {
  os << mint.get_value();
  return os;
}
template <class UnsignedInteger, UnsignedInteger Mod>
std::istream &operator>>(std::istream &is, modint<UnsignedInteger, Mod> &mint) {
  UnsignedInteger t;
  is >> t;
  mint = modint<UnsignedInteger, Mod>(t);
  return is;
}
} // namespace MMNMM

int main() {
  using namespace std;
  constexpr unsigned long MOD = 1000000007;
  using modint = MMNMM::modint<>;
  modint N, A, B, C;
  cin >> N >> A >> B >> C;
  const unsigned long Nn = N.get_value();
  A /= 100;
  B /= 100;
  C /= 100;
  C = -C + 1;
  C /= C * C;
  A *= C;
  B *= C;
  vector<modint> ifr(Nn, 1);
  if (Nn > 2)
    for (unsigned long i = 2; i != Nn; ++i)
      ifr[i] = ifr[MOD % i] * (MOD - MOD / i);
  modint ansA{0}, ansB{0}, Ap{1}, Bp{1}, a = C * N, bin = 1;
  for (unsigned long i = 0; i != Nn; ++i) {
    ansA |= a * bin * Ap;
    ansB |= a * bin * Bp;
    a += C;
    Ap *= A;
    Bp *= B;
    bin *= (N + i) * ifr[i + 1];
  }
  ansA.regularize();
  ansB.regularize();
  cout << ansA * Bp + ansB * Ap << endl;
  return 0;
}