Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
// const ll MOD = ;
ll mod(ll A, ll M) { return (A % M + M) % M; }
const ll INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll divceil(ll A, ll B) { return (A + (B - 1)) / B; }
ll myctoi(char C) { return C - '0'; }
char myitoc(ll N) { return '0' + N; }
#define FINALANS(A)                                                            \
  do {                                                                         \
    cout << (A) << '\n';                                                       \
    exit(0);                                                                   \
  } while (false)

class combination {
public:
  vector<ll> fac, finv, inv;

  combination(ll M) {
    fac.resize(M + 1);
    finv.resize(M + 1);
    inv.resize(M + 1);

    fac.at(0) = 1, finv.at(0) = 1, inv.at(0) = 1;
    fac.at(1) = 1, finv.at(1) = 1, inv.at(1) = 1;

    for (ll i = 2; i <= M; i++) {
      fac.at(i) = fac.at(i - 1) * i % MOD;
      inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
      finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
    }
  }

  ll P(ll N, ll K) {
    if (N < K)
      return 0;
    if (N < 0 || K < 0)
      return 0;
    return fac.at(N) * finv.at(N - K) % MOD;
  }

  ll C(ll N, ll K) {
    if (N < K)
      return 0;
    if (N < 0 || K < 0)
      return 0;
    return fac.at(N) * (finv.at(K) * finv.at(N - K) % MOD) % MOD;
  }

  ll H(ll N, ll K) { return C(N + K - 1, K); }
};

combination com(1e6 + 10);

ll s(ll r, ll c) {
  if (r == 0 || c == 0)
    return 0;

  ll ret = 0;
  for (ll i = 0; i < c; i++) {
    ret += com.C(r + 2 + i, 2 + i) + MOD - 1;
    ret %= MOD;
  }

  return ret;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  ll ans = ((s(r2, c2) + s(r1 - 1, c1 - 1)) % MOD) + MOD -
           ((s(r1 - 1, c2) + s(r2, c1 - 1)) % MOD);
  ans %= MOD;
  cout << ans << endl;

  // cerr << s(r2, c2) << " " << s(r1 - 1, c1 - 1) << endl;
  // cerr << s(r1 - 1, c2) << " " << s(r2, c1 - 1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
// const ll MOD = ;
ll mod(ll A, ll M) { return (A % M + M) % M; }
const ll INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll divceil(ll A, ll B) { return (A + (B - 1)) / B; }
ll myctoi(char C) { return C - '0'; }
char myitoc(ll N) { return '0' + N; }
#define FINALANS(A)                                                            \
  do {                                                                         \
    cout << (A) << '\n';                                                       \
    exit(0);                                                                   \
  } while (false)

class combination {
public:
  vector<ll> fac, finv, inv;

  combination(ll M) {
    fac.resize(M + 1);
    finv.resize(M + 1);
    inv.resize(M + 1);

    fac.at(0) = 1, finv.at(0) = 1, inv.at(0) = 1;
    fac.at(1) = 1, finv.at(1) = 1, inv.at(1) = 1;

    for (ll i = 2; i <= M; i++) {
      fac.at(i) = fac.at(i - 1) * i % MOD;
      inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
      finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
    }
  }

  ll P(ll N, ll K) {
    if (N < K)
      return 0;
    if (N < 0 || K < 0)
      return 0;
    return fac.at(N) * finv.at(N - K) % MOD;
  }

  ll C(ll N, ll K) {
    if (N < K)
      return 0;
    if (N < 0 || K < 0)
      return 0;
    return fac.at(N) * (finv.at(K) * finv.at(N - K) % MOD) % MOD;
  }

  ll H(ll N, ll K) { return C(N + K - 1, K); }
};

combination com(2 * (1e6 + 10));

ll s(ll r, ll c) {
  if (r == 0 || c == 0)
    return 0;

  ll ret = 0;
  for (ll i = 0; i < c; i++) {
    ret += com.C(r + 2 + i, 2 + i) + MOD - 1;
    ret %= MOD;
  }

  return ret;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  ll ans = ((s(r2, c2) + s(r1 - 1, c1 - 1)) % MOD) + MOD -
           ((s(r1 - 1, c2) + s(r2, c1 - 1)) % MOD);
  ans %= MOD;
  cout << ans << endl;

  // cerr << s(r2, c2) << " " << s(r1 - 1, c1 - 1) << endl;
  // cerr << s(r1 - 1, c2) << " " << s(r2, c1 - 1) << endl;
}