Problem p04004 (C++)

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

const ll MOD = 1000000007;
const int MX = 300010;

int N, M, K;
ll p3[MX], fact[MX], ifact[MX], inv[MX];

ll comb(int n, int r) {
  if (n < 0 || r < 0 || r > n)
    return 0;
  return fact[n] * ifact[r] % MOD * ifact[n - r] % MOD;
}

int main() {
  inv[1] = 1;
  for (int i = 2; i < MX; ++i) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
  }
  fact[0] = ifact[0] = p3[0] = 1;
  for (int i = 1; i < MX; ++i) {
    p3[i] = p3[i - 1] * 3 % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    ifact[i] = ifact[i - 1] * inv[i] % MOD;
  }

  cin >> N >> M >> K;

  if (M > K) {
    swap(M, K);
  }

  ll ret = 0;
  ll la = -1;
  ll p2 = 1;

  for (int t = N; t <= N + M + K; ++t) {
    ll u = 0;

    int tt = t - N;

    if (tt <= M) {
      u = p2;

      if (tt == M) {
        la = p2;
      } else {
        p2 = p2 * 2 % MOD;
      }
    } else if (tt <= K) {
      la = la * 2 % MOD;
      la = (la - comb(tt - 1, M)) % MOD;
      if (la < 0)
        la += MOD;
      u = la;
    } else {
      la = la * 2 % MOD;
      la = (la - comb(tt - 1, M)) % MOD;
      la = (la - comb(tt - 1, K)) % MOD;
      if (la < 0)
        la += MOD;
      u = la;
    }

    u = u * p3[N + M + K - t] % MOD * comb(t - 1, N - 1);
    ret = (ret + u) % MOD;
  }

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

const ll MOD = 1000000007;
const int MX = 1000010;

int N, M, K;
ll p3[MX], fact[MX], ifact[MX], inv[MX];

ll comb(int n, int r) {
  if (n < 0 || r < 0 || r > n)
    return 0;
  return fact[n] * ifact[r] % MOD * ifact[n - r] % MOD;
}

int main() {
  inv[1] = 1;
  for (int i = 2; i < MX; ++i) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
  }
  fact[0] = ifact[0] = p3[0] = 1;
  for (int i = 1; i < MX; ++i) {
    p3[i] = p3[i - 1] * 3 % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    ifact[i] = ifact[i - 1] * inv[i] % MOD;
  }

  cin >> N >> M >> K;

  if (M > K) {
    swap(M, K);
  }

  ll ret = 0;
  ll la = -1;
  ll p2 = 1;

  for (int t = N; t <= N + M + K; ++t) {
    ll u = 0;

    int tt = t - N;

    if (tt <= M) {
      u = p2;

      if (tt == M) {
        la = p2;
      } else {
        p2 = p2 * 2 % MOD;
      }
    } else if (tt <= K) {
      la = la * 2 % MOD;
      la = (la - comb(tt - 1, M)) % MOD;
      if (la < 0)
        la += MOD;
      u = la;
    } else {
      la = la * 2 % MOD;
      la = (la - comb(tt - 1, M)) % MOD;
      la = (la - comb(tt - 1, K)) % MOD;
      if (la < 0)
        la += MOD;
      u = la;
    }

    u = u * p3[N + M + K - t] % MOD * comb(t - 1, N - 1);
    ret = (ret + u) % MOD;
  }

  cout << ret << endl;

  return 0;
}