Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define mp make_pair
#define st(arr, a) memset(arr, a, sizeof arr)
#define nl cout << '\n'
#define bitcount(x) __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define rfor(i, a, b) for (int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define X first
#define Y second
#define N 1014159
#define vi vector<int>
#define v vector
#define die exit(0)
#define alln(arr) arr + 1, arr + 1 + n
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

inline int mul(int a, int b) { return (a * 1ll * b) % MOD; }
inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int power(int a, int b) {
  int rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
inline int inv(int a) { return power(a, MOD - 2); }

int fac[N];
int invfac[N];
void pre() {
  fac[0] = 1;
  invfac[0] = inv(1);
  forn(i, 1, N - 10) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= MOD;
    invfac[i] = inv(fac[i]);
  }
}
int ncr(int n, int r) {
  if (r > n)
    return 0;
  return mul(fac[n], mul(invfac[r], invfac[n - r]));
}

int n, m, k, arr[N];

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int ans = 0;
  forn(i, a, c) {
    int x = ncr(i + d + 1, i + 1) - ncr(i + b, i + 1);
    x += MOD;
    x %= MOD;
    ans = add(ans, x);
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  pre();
  forn(i, 1, t) {
    solve();
    nl;
  }

  return 0;
}

//       clang++ c.cpp ; ./a.out < a.txt ;

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define mp make_pair
#define st(arr, a) memset(arr, a, sizeof arr)
#define nl cout << '\n'
#define bitcount(x) __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define rfor(i, a, b) for (int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define X first
#define Y second
#define N 2014159
#define vi vector<int>
#define v vector
#define die exit(0)
#define alln(arr) arr + 1, arr + 1 + n
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

inline int mul(int a, int b) { return (a * 1ll * b) % MOD; }
inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int power(int a, int b) {
  int rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
inline int inv(int a) { return power(a, MOD - 2); }

int fac[N];
int invfac[N];
void pre() {
  fac[0] = 1;
  invfac[0] = inv(1);
  forn(i, 1, N - 10) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= MOD;
    invfac[i] = inv(fac[i]);
  }
}
int ncr(int n, int r) {
  if (r > n)
    return 0;
  return mul(fac[n], mul(invfac[r], invfac[n - r]));
}

int n, m, k, arr[N];

void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int ans = 0;
  forn(i, a, c) {
    int x = ncr(i + d + 1, i + 1) - ncr(i + b, i + 1);
    x += MOD;
    x %= MOD;
    ans = add(ans, x);
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  pre();
  forn(i, 1, t) {
    solve();
    nl;
  }

  return 0;
}

//       clang++ c.cpp ; ./a.out < a.txt ;