Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

const int maxn = 1e6 + 5;
const int MOD = 1e9 + 7;

struct mi {
  long long v;
  explicit operator long long() const { return v; }
  mi() { v = 0; }
  mi(long long _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0) {
      v += MOD;
    }
  }

  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD) {
      v -= MOD;
    }
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0) {
      v += MOD;
    }
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }

  friend mi pow(mi a, long long p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a) {
      if (p & 1) {
        ans *= a;
      }
    }
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  mi operator++(int) {
    mi temp;
    temp.v = v++;
    return temp;
  }
  mi operator--(int) {
    mi temp;
    temp.v = v--;
    return temp;
  }

  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }

  friend ostream &operator<<(ostream &os, const mi &m) {
    os << m.v;
    return os;
  }
  friend istream &operator>>(istream &is, mi &m) {
    long long x;
    is >> x;
    m.v = x;
    return is;
  }
};

mi fact[maxn];
mi ifact[maxn];

void preCalc() {
  fact[0] = 1;
  ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = i * fact[i - 1];
    ifact[i] = 1 / fact[i];
  }
}

mi nCr(int n, int r) {
  if (n < 0 or r < 0 or n < r) {
    return 0;
  }
  return fact[n] * ifact[r] * ifact[n - r];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  preCalc();

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  mi ans = 0;
  for (int c = c1; c <= c2; c++) {
    mi add = 0;
    if (c + r2 >= c) {
      mi ncr = nCr(c + r2 + 1, c + 1);
      ncr = ncr - (c - 1);
      add = ncr;
    }
    if (c + r1 > c) {
      mi ncr = nCr(c + r1 + 1 - 1, c + 1);
      ncr = ncr - (c - 1);
      add -= ncr;
    }
    ans += add;
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

const int maxn = 5e6 + 5;
const int MOD = 1e9 + 7;

struct mi {
  long long v;
  explicit operator long long() const { return v; }
  mi() { v = 0; }
  mi(long long _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0) {
      v += MOD;
    }
  }

  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD) {
      v -= MOD;
    }
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0) {
      v += MOD;
    }
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }

  friend mi pow(mi a, long long p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a) {
      if (p & 1) {
        ans *= a;
      }
    }
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  mi operator++(int) {
    mi temp;
    temp.v = v++;
    return temp;
  }
  mi operator--(int) {
    mi temp;
    temp.v = v--;
    return temp;
  }

  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }

  friend ostream &operator<<(ostream &os, const mi &m) {
    os << m.v;
    return os;
  }
  friend istream &operator>>(istream &is, mi &m) {
    long long x;
    is >> x;
    m.v = x;
    return is;
  }
};

mi fact[maxn];
mi ifact[maxn];

void preCalc() {
  fact[0] = 1;
  ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = i * fact[i - 1];
    ifact[i] = 1 / fact[i];
  }
}

mi nCr(int n, int r) {
  if (n < 0 or r < 0 or n < r) {
    return 0;
  }
  return fact[n] * ifact[r] * ifact[n - r];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  preCalc();

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  mi ans = 0;
  for (int c = c1; c <= c2; c++) {
    mi add = 0;
    if (c + r2 >= c) {
      mi ncr = nCr(c + r2 + 1, c + 1);
      ncr = ncr - (c - 1);
      add = ncr;
    }
    if (c + r1 > c) {
      mi ncr = nCr(c + r1 + 1 - 1, c + 1);
      ncr = ncr - (c - 1);
      add -= ncr;
    }
    ans += add;
  }
  cout << ans << endl;
}