Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int mod = 1e9 + 7;
const int MX = 610000;
ll inv[MX], f[MX], invf[MX], pw[MX];
void calc() {
  pw[0] = 1;
  rep(i, MX - 1) pw[i + 1] = pw[i] * 3 % mod;
  inv[0] = inv[1] = f[0] = f[1] = invf[0] = invf[1] = 1;
  for (int i = 2; i < MX; i++) {
    inv[i] = mod - mod / i * inv[mod % i] % mod;
    f[i] = f[i - 1] * i % mod;
    invf[i] = invf[i - 1] * inv[i] % mod;
  }
}
inline ll C(ll n, ll k) {
  if (k < 0 || k > n)
    return 0;
  assert(n < MX && k < MX);
  return f[n] * invf[k] % mod * invf[n - k] % mod;
}
int main() {
  calc();
  int a, b, c;
  cin >> a >> b >> c;
  ll ans = 0;
  ll sum = 1;
  int L = 0, R = 0;
  rep(i, b + c + 1) {
    /*
    ll s = 0;
    for(int j = max(0, i - c); j <= min(b, i); j++) s += C(i, j);
    */
    if (i)
      sum *= 2;
    int l = max(0, i - c), r = min(b, i);
    sum += C(i - 1, L - 1);
    sum += mod - C(i - 1, R);

    if (l > L)
      sum += mod - C(i, L);
    if (r > R)
      sum += C(i, r);

    assert(l == L || L + 1 == l);
    assert(r == R || R + 1 == r);
    // dbg(l, r, L, R, sum, s); assert(s % mod == sum % mod);

    L = l;
    R = r;
    sum %= mod;
    ans += sum * C(i + a - 1, i) % mod * pw[b + c - i] % mod;
  }
  cout << ans % mod << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int mod = 1e9 + 7;
const int MX = 910000;
ll inv[MX], f[MX], invf[MX], pw[MX];
void calc() {
  pw[0] = 1;
  rep(i, MX - 1) pw[i + 1] = pw[i] * 3 % mod;
  inv[0] = inv[1] = f[0] = f[1] = invf[0] = invf[1] = 1;
  for (int i = 2; i < MX; i++) {
    inv[i] = mod - mod / i * inv[mod % i] % mod;
    f[i] = f[i - 1] * i % mod;
    invf[i] = invf[i - 1] * inv[i] % mod;
  }
}
inline ll C(ll n, ll k) {
  if (k < 0 || k > n)
    return 0;
  assert(n < MX && k < MX);
  return f[n] * invf[k] % mod * invf[n - k] % mod;
}
int main() {
  calc();
  int a, b, c;
  cin >> a >> b >> c;
  ll ans = 0;
  ll sum = 1;
  int L = 0, R = 0;
  rep(i, b + c + 1) {
    /*
    ll s = 0;
    for(int j = max(0, i - c); j <= min(b, i); j++) s += C(i, j);
    */
    if (i)
      sum *= 2;
    int l = max(0, i - c), r = min(b, i);
    sum += C(i - 1, L - 1);
    sum += mod - C(i - 1, R);

    if (l > L)
      sum += mod - C(i, L);
    if (r > R)
      sum += C(i, r);

    assert(l == L || L + 1 == l);
    assert(r == R || R + 1 == r);
    // dbg(l, r, L, R, sum, s); assert(s % mod == sum % mod);

    L = l;
    R = r;
    sum %= mod;
    ans += sum * C(i + a - 1, i) % mod * pw[b + c - i] % mod;
  }
  cout << ans % mod << endl;

  return 0;
}