Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1000000007;
ll const N = 1e6 + 1;
ll fac[N];
ll invfac[N];
ll add(ll a, ll b) {
  ll t = (a + b);
  if (t >= p)
    t -= p;
  return t;
}
ll sub(ll a, ll b) {
  ll t = a - b + p;
  t = t % p;
  return t;
}
ll power(ll a, ll x) {
  ll r = 1;
  while (x != 0) {
    if (x % 2 == 1) {
      r = r * a;
      r %= p;
    }
    a = a * a;
    a %= p;
    x = x / 2;
  }
  return r;
}
ll mul(ll a, ll b) {
  ll t = (a * b);
  if (t >= p)
    t %= p;
  return t;
}
ll divide(ll a, ll b) { return mul(a, power(b, p - 2)); }
ll ncr(ll n, ll r) {
  if (n < r)
    return 0;
  return mul(fac[n], mul(invfac[n - r], invfac[r]));
}
ll calc(ll r, ll c) {
  if (r < 0 || c < 0)
    return 0;
  ll temp = 0;
  ll r1 = r, c1 = c;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = add(temp, ncr(r + c + 1, c + 1));
    r--;
    c--;
  }
  r = r1;
  c = c1;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = add(temp, ncr(r + c + 1, r + 1));
    r--;
    c--;
  }
  cout << temp << endl;
  r = r1;
  c = c1;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = sub(temp, ncr(r + c, r));
    r--;
    c--;
  }
  // if(r!=c)temp++;
  return temp % p;
}
int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  // cin>>T;
  while (T--) {
    ll r1, r2, c1, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    fac[0] = 1;
    for (ll i = 1; i < N; i++) {
      fac[i] = fac[i - 1] * i;
      fac[i] %= p;
    }
    for (ll i = 0; i < N; i++) {
      invfac[i] = power(fac[i], p - 2);
    }
    ll ans = 0;
    for (ll i = r1; i <= r2; i++) {
      ans = add(ans, ncr(i + c2 + 1, i + 1));
    }
    c1--;
    for (ll i = r1; i <= r2; i++) {
      ans = sub(ans, ncr(i + c1 + 1, i + 1));
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1000000007;
ll const N = 2e6 + 15;
ll fac[N];
ll invfac[N];
ll add(ll a, ll b) {
  ll t = (a + b);
  if (t >= p)
    t -= p;
  return t;
}
ll sub(ll a, ll b) {
  ll t = a - b + p;
  t = t % p;
  return t;
}
ll power(ll a, ll x) {
  ll r = 1;
  while (x != 0) {
    if (x % 2 == 1) {
      r = r * a;
      r %= p;
    }
    a = a * a;
    a %= p;
    x = x / 2;
  }
  return r;
}
ll mul(ll a, ll b) {
  ll t = (a * b);
  if (t >= p)
    t %= p;
  return t;
}
ll divide(ll a, ll b) { return mul(a, power(b, p - 2)); }
ll ncr(ll n, ll r) {
  if (n < r)
    return 0;
  return mul(fac[n], mul(invfac[n - r], invfac[r]));
}
ll calc(ll r, ll c) {
  if (r < 0 || c < 0)
    return 0;
  ll temp = 0;
  ll r1 = r, c1 = c;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = add(temp, ncr(r + c + 1, c + 1));
    r--;
    c--;
  }
  r = r1;
  c = c1;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = add(temp, ncr(r + c + 1, r + 1));
    r--;
    c--;
  }
  cout << temp << endl;
  r = r1;
  c = c1;
  while (1) {
    if (r < 0 || c < 0)
      break;
    temp = sub(temp, ncr(r + c, r));
    r--;
    c--;
  }
  // if(r!=c)temp++;
  return temp % p;
}
int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  // cin>>T;
  while (T--) {
    ll r1, r2, c1, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    fac[0] = 1;
    for (ll i = 1; i < N; i++) {
      fac[i] = fac[i - 1] * i;
      fac[i] %= p;
    }
    for (ll i = 0; i < N; i++) {
      invfac[i] = power(fac[i], p - 2);
    }
    ll ans = 0;
    for (ll i = r1; i <= r2; i++) {
      ans = add(ans, ncr(i + c2 + 1, i + 1));
    }
    c1--;
    for (ll i = r1; i <= r2; i++) {
      ans = sub(ans, ncr(i + c1 + 1, i + 1));
    }
    cout << ans << endl;
  }
  return 0;
}