Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/stack:200000000"]

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v)                                                                \
  for (auto &x : v)                                                            \
  cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

#define int ll

using namespace std;

#ifdef __LOCAL
#define dbg(x) cerr << #x << " : " << x << '\n'
const int maxn = 20;
#else
#define dbg(x)
const int maxn = 1e6 + 20;
#endif

// tg: @galebickosikasa

const ll inf = (ll)2e9;
const ld pi = asin(1) * 2;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

ll mult(ll a, ll b) { return a * b % mod; }

ll add(ll a, ll b) {
  int c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}

ll powmod(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return mult(a, powmod(a, n - 1));
  ll b = powmod(a, n / 2);
  return mult(b, b);
}

ll inv(int a) { return powmod(a, mod - 2); }

int f[maxn], rev[maxn];

int C(int n, int k) { return mult(f[n], mult(rev[k], rev[n - k])); }

int S(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  return C(i + j + 2, i + 1);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  f[0] = 1;
  fl(i, 1, maxn) {
    f[i] = mult(f[i - 1], i);
    rev[i] = inv(f[i]);
  }
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ans = S(r2, c2) - S(r1 - 1, c2) - S(r2, c1 - 1) + S(r1 - 1, c1 - 1);
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans;
}

Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/stack:200000000"]

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v)                                                                \
  for (auto &x : v)                                                            \
  cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

#define int ll

using namespace std;

#ifdef __LOCAL
#define dbg(x) cerr << #x << " : " << x << '\n'
const int maxn = 20;
#else
#define dbg(x)
const int maxn = 2e6 + 20;
#endif

// tg: @galebickosikasa

const ll inf = (ll)2e9;
const ld pi = asin(1) * 2;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

ll mult(ll a, ll b) { return a * b % mod; }

ll add(ll a, ll b) {
  int c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}

ll powmod(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return mult(a, powmod(a, n - 1));
  ll b = powmod(a, n / 2);
  return mult(b, b);
}

ll inv(int a) { return powmod(a, mod - 2); }

int f[maxn], rev[maxn];

int C(int n, int k) { return mult(f[n], mult(rev[k], rev[n - k])); }

int S(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  return C(i + j + 2, i + 1);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  f[0] = 1;
  fl(i, 1, maxn) {
    f[i] = mult(f[i - 1], i);
    rev[i] = inv(f[i]);
  }
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ans = S(r2, c2) - S(r1 - 1, c2) - S(r2, c1 - 1) + S(r1 - 1, c1 - 1);
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans;
}