Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <list>

// hloya template v25

// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░

#include <bits/stdc++.h>
#include <valarray>
using namespace std;

bool dbg = 0;

clock_t start_time = clock();
#define current_time                                                           \
  fixed << setprecision(6) << (ld)(clock() - start_time) / CLOCKS_PER_SEC

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()

#define ll long long
#define ld long double
#define pii pair<int, int>
#define umap unordered_map<int, int>

#define files1 freopen("input.txt", "r", stdin)
#define files2 freopen("output.txt", "w", stdout)
#define files                                                                  \
  files1;                                                                      \
  files2
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#define endl '\n'
#define ln(i, n) " \n"[(i) == (n)-1]

void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}

template <typename T> string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}

template <typename T> void amax(T &x, T y) { x = max(x, y); }

template <typename T> void amin(T &x, T y) { x = min(x, y); }

// inline int popcount(int x){
//     int count = 0;
//     __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):);
//     return count;
// }

template <typename T> T input() {
  T ans = 0, m = 1;
  char c = ' ';

  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }

  if (c == '-')
    m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}

template <typename T> void read(T &a) { a = input<T>(); }
template <typename T> void read(T &a, T &b) { read(a), read(b); }
template <typename T> void read(T &a, T &b, T &c) { read(a, b), read(c); }
template <typename T> void read(T &a, T &b, T &c, T &d) {
  read(a, b), read(c, d);
}

const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)1e6 + 12, base = 1e9 + 7;
const ll llinf = 1e18 + 5;
const double PI = acos(-1.0);

const int mod = base;

template <typename T> T binpow(T n, T s) {
  if (s <= 0)
    return 1LL;
  if (s % 2 == 0) {
    T b = binpow(n, s / 2);
    return (1LL * b * b) % mod;
  } else {
    return (1LL * binpow(n, s - 1) * n) % mod;
  }
}

int fact[maxn], inv[maxn];

int main() {
  fast_io;

  fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = 1ll * fact[i - 1] * i % mod;
  }
  inv[maxn - 1] = binpow(fact[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  }

  auto c = [&](int n, int k) {
    return 1ll * fact[n] * inv[k] % mod * inv[n - k] % mod;
  };

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  int res = 0;
  for (int j = c1; j <= c2; j++) {
    int l = j + r1, r = j + r2;
    (res += (c(r + 1, j + 1) - c(l, j + 1) + mod) % mod) %= mod;
  }
  cout << res;
  return 0;
}

Fixed submission:
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <list>

// hloya template v25

// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░

#include <bits/stdc++.h>
#include <valarray>
using namespace std;

bool dbg = 0;

clock_t start_time = clock();
#define current_time                                                           \
  fixed << setprecision(6) << (ld)(clock() - start_time) / CLOCKS_PER_SEC

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()

#define ll long long
#define ld long double
#define pii pair<int, int>
#define umap unordered_map<int, int>

#define files1 freopen("input.txt", "r", stdin)
#define files2 freopen("output.txt", "w", stdout)
#define files                                                                  \
  files1;                                                                      \
  files2
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#define endl '\n'
#define ln(i, n) " \n"[(i) == (n)-1]

void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}

template <typename T> string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}

template <typename T> void amax(T &x, T y) { x = max(x, y); }

template <typename T> void amin(T &x, T y) { x = min(x, y); }

// inline int popcount(int x){
//     int count = 0;
//     __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):);
//     return count;
// }

template <typename T> T input() {
  T ans = 0, m = 1;
  char c = ' ';

  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }

  if (c == '-')
    m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}

template <typename T> void read(T &a) { a = input<T>(); }
template <typename T> void read(T &a, T &b) { read(a), read(b); }
template <typename T> void read(T &a, T &b, T &c) { read(a, b), read(c); }
template <typename T> void read(T &a, T &b, T &c, T &d) {
  read(a, b), read(c, d);
}

const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e6 + 12, base = 1e9 + 7;
const ll llinf = 1e18 + 5;
const double PI = acos(-1.0);

const int mod = base;

template <typename T> T binpow(T n, T s) {
  if (s <= 0)
    return 1LL;
  if (s % 2 == 0) {
    T b = binpow(n, s / 2);
    return (1LL * b * b) % mod;
  } else {
    return (1LL * binpow(n, s - 1) * n) % mod;
  }
}

int fact[maxn], inv[maxn];

int main() {
  fast_io;

  fact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = 1ll * fact[i - 1] * i % mod;
  }
  inv[maxn - 1] = binpow(fact[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  }

  auto c = [&](int n, int k) {
    return 1ll * fact[n] * inv[k] % mod * inv[n - k] % mod;
  };

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  int res = 0;
  for (int j = c1; j <= c2; j++) {
    int l = j + r1, r = j + r2;
    (res += (c(r + 1, j + 1) - c(l, j + 1) + mod) % mod) %= mod;
  }
  cout << res;
  return 0;
}