Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define optimiza_io                                                            \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
#define fileinput(s) freopen(s, "r", stdin)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
typedef long long ll;

const ll mod = 1000000007;

vector<int> fact;
vector<int> inv;

void printArr(vector<int> arr, int n) {
  cout << "\n------------------------------------\n";

  for (int i = 0; i < n; i++)
    cout << arr[i] << " ";

  cout << "\n------------------------------------\n";
}

ll modpow(ll a, ll b) {
  ll c = 1;

  while (b) {
    if (b & 1)
      c = 1LL * c * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }

  return c;
}

ll ncr(ll n, ll k) { return 1LL * fact[n] * inv[k] % mod * inv[n - k] % mod; }

void init() {
  int maxn = 1000000 + 10;

  fact.assign(maxn, 1);
  inv.assign(maxn, 1);

  for (int i = 1; i < maxn; ++i)
    fact[i] = 1LL * fact[i - 1] * i % mod;

  inv[maxn - 1] = modpow(fact[maxn - 1], mod - 2);

  for (int i = maxn - 2; i >= 0; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;

  // printArr(fact,10);
}
int main() {
  optimiza_io

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

  init();

  ll ans1 = 0;

  ll p1 = ncr(r2 + c2 + 2, c2 + 1);
  ll p2 = ncr(r1 + c2 + 1, c2 + 1);
  ll p3 = ncr(r2 + c1 + 1, c1);
  ll p4 = ncr(c1 + r1, c1);

  ans1 = p1 + p4 - p3 - p2;

  while (ans1 <= 0)
    ans1 += mod;

  ans1 = (ans1 % mod);

  cout << ans1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define optimiza_io                                                            \
  cin.tie(0);                                                                  \
  ios_base::sync_with_stdio(0);
#define fileinput(s) freopen(s, "r", stdin)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
typedef long long ll;

const ll mod = 1000000007;

vector<int> fact;
vector<int> inv;

void printArr(vector<int> arr, int n) {
  cout << "\n------------------------------------\n";

  for (int i = 0; i < n; i++)
    cout << arr[i] << " ";

  cout << "\n------------------------------------\n";
}

ll modpow(ll a, ll b) {
  ll c = 1;

  while (b) {
    if (b & 1)
      c = 1LL * c * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }

  return c;
}

ll ncr(ll n, ll k) { return 1LL * fact[n] * inv[k] % mod * inv[n - k] % mod; }

void init() {
  int maxn = 2000000 + 10;

  fact.assign(maxn, 1);
  inv.assign(maxn, 1);

  for (int i = 1; i < maxn; ++i)
    fact[i] = 1LL * fact[i - 1] * i % mod;

  inv[maxn - 1] = modpow(fact[maxn - 1], mod - 2);

  for (int i = maxn - 2; i >= 0; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;

  // printArr(fact,10);
}
int main() {
  optimiza_io

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

  init();

  ll ans1 = 0;

  ll p1 = ncr(r2 + c2 + 2, c2 + 1);
  ll p2 = ncr(r1 + c2 + 1, c2 + 1);
  ll p3 = ncr(r2 + c1 + 1, c1);
  ll p4 = ncr(c1 + r1, c1);

  ans1 = p1 + p4 - p3 - p2;

  while (ans1 <= 0)
    ans1 += mod;

  ans1 = (ans1 % mod);

  cout << ans1 << endl;
  return 0;
}