Problem p02782 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

// mod
int mod = 1e9 + 7;

struct mint {
  unsigned x;

  mint() : x(0) {}

  mint(signed sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  mint(signed long long sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  int get() const { return (int)x; }

  mint &operator+=(mint that) {
    if ((x += that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(mint that) {
    if ((x += mod - that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(mint that) {
    x = (unsigned long long)x * that.x % mod;
    return *this;
  }

  mint &operator/=(mint that) { return *this *= that.inverse(); }

  mint operator+(mint that) const { return mint(*this) += that; }

  mint operator-(mint that) const { return mint(*this) -= that; }

  mint operator*(mint that) const { return mint(*this) *= that; }

  mint operator/(mint that) const { return mint(*this) /= that; }

  mint inverse() const {
    long long a = x, b = mod, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return mint(u);
  }

  bool operator==(mint that) const { return x == that.x; }

  bool operator!=(mint that) const { return x != that.x; }

  mint operator-() const {
    mint t;
    t.x = x == 0 ? 0 : mod - x;
    return t;
  }
};

struct combination {
  vector<mint> fact, ifact;

  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inverse();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }

  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

class FManyManyPaths {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;

    combination cb(1000005);

    mint res = 0;
    FOR(r, r1, r2) { res += cb(c2 + r + 1, r + 1) - cb(c1 + r, r + 1); }
    cout << res.get() << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  FManyManyPaths solver;
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

// mod
int mod = 1e9 + 7;

struct mint {
  unsigned x;

  mint() : x(0) {}

  mint(signed sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  mint(signed long long sig) { x = sig < 0 ? sig % mod + mod : sig % mod; }

  int get() const { return (int)x; }

  mint &operator+=(mint that) {
    if ((x += that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(mint that) {
    if ((x += mod - that.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(mint that) {
    x = (unsigned long long)x * that.x % mod;
    return *this;
  }

  mint &operator/=(mint that) { return *this *= that.inverse(); }

  mint operator+(mint that) const { return mint(*this) += that; }

  mint operator-(mint that) const { return mint(*this) -= that; }

  mint operator*(mint that) const { return mint(*this) *= that; }

  mint operator/(mint that) const { return mint(*this) /= that; }

  mint inverse() const {
    long long a = x, b = mod, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return mint(u);
  }

  bool operator==(mint that) const { return x == that.x; }

  bool operator!=(mint that) const { return x != that.x; }

  mint operator-() const {
    mint t;
    t.x = x == 0 ? 0 : mod - x;
    return t;
  }
};

struct combination {
  vector<mint> fact, ifact;

  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inverse();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }

  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

class FManyManyPaths {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;

    combination cb(3000005);

    mint res = 0;
    FOR(r, r1, r2) { res += cb(c2 + r + 1, r + 1) - cb(c1 + r, r + 1); }
    cout << res.get() << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  FManyManyPaths solver;
  solver.solve(in, out);
  return 0;
}