Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

ll inv(ll x) {
  ll t = MOD - 2;
  ll ans = 1;
  while (t) {
    if (t & 1)
      ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    t >>= 1;
  }
  return ans;
}

#define N_MAX 220010
ll fact[N_MAX];

ll comb(ll a, ll b) {
  if (a < b)
    return 0;
  return fact[a] * inv(fact[b]) % MOD * inv(fact[a - b]) % MOD;
}

int main() {
  fact[0] = 1;
  REPR(i, N_MAX) fact[i] = fact[i - 1] * i % MOD;
  ll n, m, k;
  cin >> n >> m >> k;
  ll result = 0;
  REP(i, k + 1) {
    ll l = i;
    ll r = n + i;
    ll d = k - i;
    ll u = m + (k - i);
    // http://kakuritsu.com/catalan.html
    ll hor = FIX(comb(r + l, r) - comb(r + l, r + 1));
    ll ver = FIX(comb(u + d, u) - comb(u + d, u + 1));
    ll v = hor * ver % MOD * comb(r + l + u + d, r + l) % MOD;
    result += v;
    result %= MOD;
  }
  cout << result << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

ll inv(ll x) {
  ll t = MOD - 2;
  ll ans = 1;
  while (t) {
    if (t & 1)
      ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    t >>= 1;
  }
  return ans;
}

#define N_MAX 210010
ll fact[N_MAX];

ll comb(ll a, ll b) {
  if (a < b)
    return 0;
  return fact[a] * inv(fact[b]) % MOD * inv(fact[a - b]) % MOD;
}

int main() {
  fact[0] = 1;
  REPR(i, N_MAX) fact[i] = fact[i - 1] * i % MOD;
  ll n, m, k;
  cin >> n >> m >> k;
  ll result = 0;
  REP(i, k + 1) {
    ll l = i;
    ll r = n + i;
    ll d = k - i;
    ll u = m + (k - i);
    // http://kakuritsu.com/catalan.html
    ll hor = FIX(comb(r + l, r) - comb(r + l, r + 1));
    ll ver = FIX(comb(u + d, u) - comb(u + d, u + 1));
    ll v = hor * ver % MOD * comb(r + l + u + d, r + l) % MOD;
    result += v;
    result %= MOD;
  }
  cout << result << endl;
  return 0;
}