Problem p02782 (C++)

Buggy submission (status: Runtime Error):
// in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)

const int maxn = 1e6 + 50;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
ll r1;
ll r2;
ll c1;
ll c2;
ll fact[maxn];
ll inv[maxn];
ll ans;
ll combi(ll a, ll b) {
  if (a < b)
    return 0;
  ll cp = fact[a];
  cp = (cp * inv[b]) % mod;
  cp = (cp * inv[a - b]) % mod;
  return cp;
}
int main() {
  migmig;
  cin >> r1 >> c1 >> r2 >> c2;
  fact[0] = fact[1] = 1;
  inv[1] = 1;
  inv[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv[i] = poww(fact[i], mod - 2, mod) % mod;
  }
  // cout<<combi(3,2);
  // cout
  for (int i = r1; i <= r2; i++) {
    ll x = combi(i + 1 + c2, c2) % mod;
    ll y = combi(i + 1 + c1 - 1, c1 - 1) % mod;
    ans = (ans + x - y + mod) % mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)

const int maxn = 2e6 + 50;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
ll r1;
ll r2;
ll c1;
ll c2;
ll fact[maxn];
ll inv[maxn];
ll ans;
ll combi(ll a, ll b) {
  if (a < b)
    return 0;
  ll cp = fact[a];
  cp = (cp * inv[b]) % mod;
  cp = (cp * inv[a - b]) % mod;
  return cp;
}
int main() {
  migmig;
  cin >> r1 >> c1 >> r2 >> c2;
  fact[0] = fact[1] = 1;
  inv[1] = 1;
  inv[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv[i] = poww(fact[i], mod - 2, mod) % mod;
  }
  // cout<<combi(3,2);
  // cout
  for (int i = r1; i <= r2; i++) {
    ll x = combi(i + 1 + c2, c2) % mod;
    ll y = combi(i + 1 + c1 - 1, c1 - 1) % mod;
    ans = (ans + x - y + mod) % mod;
  }
  cout << ans << endl;
  return 0;
}