Problem p02782 (C++)

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

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*
 */

using vi = vector<int>;
using vvi = vector<vi>;
using P = pair<int, int>;

const int mod = 1e9 + 7;

template <int M> class Combination {
public:
  int n;
  vector<int> fac, finv;

  Combination(int n_) : n(n_), fac(n_), finv(n_) {
    fac[0] = 1;
    for (int i = 1; i < n; i++)
      fac[i] = fac[i - 1] * i % M;
    finv[n - 1] = pow(fac[n - 1], M - 2);
    for (int i = n - 1; i > 0; i--)
      finv[i - 1] = finv[i] * i % M;
  }

  int pow(int a, int b) {
    if (b == 0)
      return 1;
    return pow(a * a % M, b / 2) * (b % 2 ? a : 1) % M;
  }

  int C(int a, int b) {
    if (b < 0 or a < b)
      return 0;
    return fac[a] * finv[a - b] % M * finv[b] % M;
  }

  int P(int a, int b) {
    if (b < 0 or a < b)
      return 0;
    return fac[a] * finv[a - b] % M;
  }

  int H(int a, int b) {
    if (a == 0 and b == 0)
      return 1;
    return C(a + b - 1, b);
  }
};

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

  Combination<mod> comb(201010);
  int ans = 0;
  rep(r, r1, r2 + 1) {
    int tmp1 =
        comb.fac[r + c2 + 1] * comb.finv[r + 1] % mod * comb.finv[c2] % mod;
    int tmp2 =
        comb.fac[r + c1] * comb.finv[r + 1] % mod * comb.finv[c1 - 1] % mod;
    ans += (tmp1 - tmp2 + mod) % mod;
    ans %= mod;
  }

  cout << ans << endl;

  return 0;
}


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

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*
 */

using vi = vector<int>;
using vvi = vector<vi>;
using P = pair<int, int>;

const int mod = 1e9 + 7;

template <int M> class Combination {
public:
  int n;
  vector<int> fac, finv;

  Combination(int n_) : n(n_), fac(n_), finv(n_) {
    fac[0] = 1;
    for (int i = 1; i < n; i++)
      fac[i] = fac[i - 1] * i % M;
    finv[n - 1] = pow(fac[n - 1], M - 2);
    for (int i = n - 1; i > 0; i--)
      finv[i - 1] = finv[i] * i % M;
  }

  int pow(int a, int b) {
    if (b == 0)
      return 1;
    return pow(a * a % M, b / 2) * (b % 2 ? a : 1) % M;
  }

  int C(int a, int b) {
    if (b < 0 or a < b)
      return 0;
    return fac[a] * finv[a - b] % M * finv[b] % M;
  }

  int P(int a, int b) {
    if (b < 0 or a < b)
      return 0;
    return fac[a] * finv[a - b] % M;
  }

  int H(int a, int b) {
    if (a == 0 and b == 0)
      return 1;
    return C(a + b - 1, b);
  }
};

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

  Combination<mod> comb(2000010);
  int ans = 0;
  rep(r, r1, r2 + 1) {
    int tmp1 =
        comb.fac[r + c2 + 1] * comb.finv[r + 1] % mod * comb.finv[c2] % mod;
    int tmp2 =
        comb.fac[r + c1] * comb.finv[r + 1] % mod * comb.finv[c1 - 1] % mod;
    ans += (tmp1 - tmp2 + mod) % mod;
    ans %= mod;
  }

  cout << ans << endl;

  return 0;
}