Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pci pair<char, int>
#define ppll pair<pll, pll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mll map<ll, ll>
#define fastmap gp_hash_table
#define cd complex<double>
#define vcd vector<cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
using namespace __gnu_pbds;
ll inv[3000005], fact[3000005], invfact[3000005];
ll mod = 1e9 + 7;
ll nck(ll n, ll k) { return fact[n] * invfact[k] % mod * invfact[n - k] % mod; }
ll binpow(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = binpow(a, b / 2);
  x *= x;
  x %= mod;
  if (b % 2)
    return (a * x) % mod;
  else
    return x;
}
ll bindiv(ll a, ll b) { return a * binpow(b, mod - 2) % mod; }
int main() {
  inv[1] = 1;
  for (ll i = 2; i < 3000005; i++)
    inv[i] = bindiv(1LL, i);
  fact[0] = invfact[0] = 1;
  for (ll i = 1; i < 3000005; i++) {
    fact[i] = fact[i - 1] * i % mod;
    invfact[i] = invfact[i - 1] * inv[i] % mod;
  }
  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  cout << ((nck(r2 + c2 + 2, c2 + 1) - nck(r1 + c2 + 1, c2 + 1) -
            nck(r2 + c1 + 1, c1) + nck(r1 + c1, c1)) %
               mod +
           mod) %
              mod;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pci pair<char, int>
#define ppll pair<pll, pll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mll map<ll, ll>
#define fastmap gp_hash_table
#define cd complex<double>
#define vcd vector<cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
using namespace __gnu_pbds;
ll inv[3000005], fact[3000005], invfact[3000005];
ll mod = 1e9 + 7;
ll nck(ll n, ll k) { return fact[n] * invfact[k] % mod * invfact[n - k] % mod; }
ll binpow(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = binpow(a, b / 2);
  x *= x;
  x %= mod;
  if (b % 2)
    return (a * x) % mod;
  else
    return x;
}
ll bindiv(ll a, ll b) { return a * binpow(b, mod - 2) % mod; }
int main() {
  inv[1] = 1;
  for (ll i = 2; i < 3000005; i++)
    inv[i] = mod - mod / i * inv[mod % i] % mod;
  fact[0] = invfact[0] = 1;
  for (ll i = 1; i < 3000005; i++) {
    fact[i] = fact[i - 1] * i % mod;
    invfact[i] = invfact[i - 1] * inv[i] % mod;
  }
  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  cout << ((nck(r2 + c2 + 2, c2 + 1) - nck(r1 + c2 + 1, c2 + 1) -
            nck(r2 + c1 + 1, c1) + nck(r1 + c1, c1)) %
               mod +
           mod) %
              mod;
  return 0;
}