Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define PI 3.141592653589793238
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef complex<double> com;
template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

ll mod = 1000000007;
ll modsize = 600010;

ll modinv(ll a) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll modpow(ll a, ll b) {
  ll ans = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

vector<ll> fac(modsize + 5);
vector<ll> inv(modsize + 5);
vector<ll> facinv(modsize + 5);

void modcalc() {
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  facinv[0] = facinv[1] = 1;
  for (int i = 2; i < modsize; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    facinv[i] = facinv[i - 1] * inv[i] % mod;
  }
}

ll modcomb(ll n, ll k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}

ll modperm(ll n, ll k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  return fac[n] * facinv[n - k] % mod;
}

ll modhom(ll n, ll k) {
  if (n < 0 || k < 0)
    return 0;
  if (n == 0 && k == 0)
    return 1;
  return modcomb(n + k - 1, k);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  modcalc();

  int a, b, c;
  cin >> a >> b >> c;
  int ans = 0;
  int p = 1;
  rep(i, b + c + 1) {
    int cnt = modpow(3, b + c - i);
    cnt *= modcomb(a + i - 1, a - 1);
    cnt %= mod;
    cnt *= p;
    cnt %= mod;
    ans += cnt;
    if (ans > mod)
      ans -= mod;
    p *= 2;
    p += 2 * mod;
    p -= modcomb(i, b);
    p -= modcomb(i, c);
    p %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define PI 3.141592653589793238
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef complex<double> com;
template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

ll mod = 1000000007;
ll modsize = 900010;

ll modinv(ll a) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll modpow(ll a, ll b) {
  ll ans = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

vector<ll> fac(modsize + 5);
vector<ll> inv(modsize + 5);
vector<ll> facinv(modsize + 5);

void modcalc() {
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  facinv[0] = facinv[1] = 1;
  for (int i = 2; i < modsize; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    facinv[i] = facinv[i - 1] * inv[i] % mod;
  }
}

ll modcomb(ll n, ll k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}

ll modperm(ll n, ll k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  return fac[n] * facinv[n - k] % mod;
}

ll modhom(ll n, ll k) {
  if (n < 0 || k < 0)
    return 0;
  if (n == 0 && k == 0)
    return 1;
  return modcomb(n + k - 1, k);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  modcalc();

  int a, b, c;
  cin >> a >> b >> c;
  int ans = 0;
  int p = 1;
  rep(i, b + c + 1) {
    int cnt = modpow(3, b + c - i);
    cnt *= modcomb(a + i - 1, a - 1);
    cnt %= mod;
    cnt *= p;
    cnt %= mod;
    ans += cnt;
    if (ans > mod)
      ans -= mod;
    p *= 2;
    p += 2 * mod;
    p -= modcomb(i, b);
    p -= modcomb(i, c);
    p %= mod;
  }
  cout << ans << endl;
}