Problem p02555 (C++)

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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;

long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  assert(b != 0);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

class ModInt {
  int _mod;
  long long x;

public:
  ModInt() {}
  ModInt(long long _x, int m) {
    _mod = m;
    x = _x % m;
    if (x < 0)
      x += m;
  }

  int val() { return (int)x; }

  int mod() { return _mod; }

  ModInt &operator++() {
    x++;
    if (x == _mod)
      x = 0;
    return *this;
  }

  ModInt &operator--() {
    if (x == 0)
      x = _mod;
    x--;
    return *this;
  }

  ModInt &operator+=(const ModInt &rhs) {
    x += rhs.x;
    if (x >= _mod)
      x -= _mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &rhs) {
    x -= rhs.x;
    if (x < 0)
      x += _mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &rhs) {
    x *= rhs.x;
    if (x >= _mod)
      x = x % _mod;
    return *this;
  }

  ModInt operator+() const { return *this; }

  ModInt operator-() const { return ModInt(-x, _mod); }

  friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) += rhs;
  }

  friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) -= rhs;
  }

  friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) *= rhs;
  }

  ModInt pow(long long n) const {
    if (n == 0) {
      return ModInt(1, _mod);
    } else if (n % 2 == 1) {
      ModInt tmp = pow((n - 1) / 2);
      return tmp * tmp * ModInt(x, _mod);
    } else {
      ModInt tmp = pow(n / 2);
      return tmp * tmp;
    }
  }

  ModInt inv() const {
    assert(gcd(x, _mod) == 1);
    assert(_mod >= 2);
    return pow(_mod - 2);
  }

  ModInt &operator/=(const ModInt &rhs) {
    return (*this) = (*this) * rhs.inv();
  }

  friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) /= rhs;
  }

  friend bool operator==(const ModInt &lhs, const ModInt &rhs) {
    return lhs.x == rhs.x;
  }

  friend bool operator!=(const ModInt &lhs, const ModInt &rhs) {
    return lhs.x != rhs.x;
  }
};

class ModIntGen {
  int modn;

public:
  ModIntGen() {}
  ModIntGen(int n) {
    assert(0 < n && n < INT_MAX);
    modn = n;
  }

  ModInt g(long long x) { return ModInt(x, modn); }
};

int main() {

  int s;
  cin >> s;

  ModIntGen mg(MODN);

  vector<ModInt> fact(s + s);
  fact[0] = mg.g(1);

  for (int i = 1; i <= s + s; i++) {
    fact[i] = fact[i - 1] * mg.g(i);
  }

  ModInt ans = mg.g(0);

  for (int i = 1; i <= s; i++) {
    int rest = s - i * 3;
    if (rest < 0)
      continue;

    ans += fact[rest + i - 1] / fact[rest] / fact[i - 1];
  }

  cout << ans.val() << endl;

  return 0;
}


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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;

long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  assert(b != 0);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

class ModInt {
  int _mod;
  long long x;

public:
  ModInt() {}
  ModInt(long long _x, int m) {
    _mod = m;
    x = _x % m;
    if (x < 0)
      x += m;
  }

  int val() { return (int)x; }

  int mod() { return _mod; }

  ModInt &operator++() {
    x++;
    if (x == _mod)
      x = 0;
    return *this;
  }

  ModInt &operator--() {
    if (x == 0)
      x = _mod;
    x--;
    return *this;
  }

  ModInt &operator+=(const ModInt &rhs) {
    x += rhs.x;
    if (x >= _mod)
      x -= _mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &rhs) {
    x -= rhs.x;
    if (x < 0)
      x += _mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &rhs) {
    x *= rhs.x;
    if (x >= _mod)
      x = x % _mod;
    return *this;
  }

  ModInt operator+() const { return *this; }

  ModInt operator-() const { return ModInt(-x, _mod); }

  friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) += rhs;
  }

  friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) -= rhs;
  }

  friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) *= rhs;
  }

  ModInt pow(long long n) const {
    if (n == 0) {
      return ModInt(1, _mod);
    } else if (n % 2 == 1) {
      ModInt tmp = pow((n - 1) / 2);
      return tmp * tmp * ModInt(x, _mod);
    } else {
      ModInt tmp = pow(n / 2);
      return tmp * tmp;
    }
  }

  ModInt inv() const {
    assert(gcd(x, _mod) == 1);
    assert(_mod >= 2);
    return pow(_mod - 2);
  }

  ModInt &operator/=(const ModInt &rhs) {
    return (*this) = (*this) * rhs.inv();
  }

  friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
    return ModInt(lhs) /= rhs;
  }

  friend bool operator==(const ModInt &lhs, const ModInt &rhs) {
    return lhs.x == rhs.x;
  }

  friend bool operator!=(const ModInt &lhs, const ModInt &rhs) {
    return lhs.x != rhs.x;
  }
};

class ModIntGen {
  int modn;

public:
  ModIntGen() {}
  ModIntGen(int n) {
    assert(0 < n && n < INT_MAX);
    modn = n;
  }

  ModInt g(long long x) { return ModInt(x, modn); }
};

int main() {

  int s;
  cin >> s;

  ModIntGen mg(MODN);

  vector<ModInt> fact(s + s + 1);
  fact[0] = mg.g(1);

  for (int i = 1; i <= s + s; i++) {
    fact[i] = fact[i - 1] * mg.g(i);
  }

  ModInt ans = mg.g(0);

  for (int i = 1; i <= s; i++) {
    int rest = s - i * 3;
    if (rest < 0)
      continue;

    ans += fact[rest + i - 1] / fact[rest] / fact[i - 1];
  }

  cout << ans.val() << endl;

  return 0;
}