Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define cina(a, n)                                                             \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define couta(a, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define fauto(ar)                                                              \
  for (auto x : ar)                                                            \
    cout << x << " ";
#define mod 1000000007
#define siz 1000005
#define f first
#define s second
#define pb push_back
#define endl "\n"
#define ci cin.ignore()
using namespace std;
ll fact[siz], inv[siz];
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    n & 1 ? res *= a, res %= mod : 1;
    a *= a;
    a %= mod;
    n >>= 1;
  }
  return res;
}
ll ncr(ll n, ll r) {
  ll val = fact[n] * inv[r] % mod;
  val = val * inv[n - r] % mod;
  return val;
}
int main() {
  ll n, r1, r2, c1, c2, i;

  cin >> r1 >> c1 >> r2 >> c2;

  fact[0] = 1;
  for (i = 1; i < siz; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }

  inv[siz - 1] = power(fact[siz - 1], mod - 2);

  for (i = siz - 2; i >= 0; i--) {
    inv[i] = (i + 1) * inv[i + 1] % mod;
  }

  ll op1 = ncr(r2 + c2 + 2, r2 + 1) - ncr(r2 + c1 + 1, r2 + 1) -
           ncr(r1 + c2 + 1, r1) + ncr(r1 + c1, r1);

  op1 %= mod;
  op1 += mod;
  op1 %= mod;

  cout << op1;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define cina(a, n)                                                             \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define couta(a, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define fauto(ar)                                                              \
  for (auto x : ar)                                                            \
    cout << x << " ";
#define mod 1000000007
#define siz 2000005
#define f first
#define s second
#define pb push_back
#define endl "\n"
#define ci cin.ignore()
using namespace std;
ll fact[siz], inv[siz];
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    n & 1 ? res *= a, res %= mod : 1;
    a *= a;
    a %= mod;
    n >>= 1;
  }
  return res;
}
ll ncr(ll n, ll r) {
  ll val = fact[n] * inv[r] % mod;
  val = val * inv[n - r] % mod;
  return val;
}
int main() {
  ll n, r1, r2, c1, c2, i;

  cin >> r1 >> c1 >> r2 >> c2;

  fact[0] = 1;
  for (i = 1; i < siz; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }

  inv[siz - 1] = power(fact[siz - 1], mod - 2);

  for (i = siz - 2; i >= 0; i--) {
    inv[i] = (i + 1) * inv[i + 1] % mod;
  }

  ll op1 = ncr(r2 + c2 + 2, r2 + 1) - ncr(r2 + c1 + 1, r2 + 1) -
           ncr(r1 + c2 + 1, r1) + ncr(r1 + c1, r1);

  op1 %= mod;
  op1 += mod;
  op1 %= mod;

  cout << op1;
}