Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define int long long
const int mod = 1e9 + 7;
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
struct nd {
  int l, r;
  int d;
  int v;
};
int fac[300000], inv[3000000];
int C(int n, int m) {
  if (n < 0 || m > n)
    return 0;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
nd cal(nd t, int d, int l, int r) {
  // nd res = t;
  if (r - l + 1 <= 4) {
    rep(i, t.l, t.r) t.v = (t.v - C(t.d, i)) % mod;
    t.d = d;
    t.l = l;
    t.r = r;
    rep(i, l, r) t.v = (t.v + C(t.d, i)) % mod;
  } else {
    t.v = (2 * t.v % mod - C(t.d, t.l) - C(t.d, t.r)) % mod;
    t.v = (t.v + C(d, t.l)) % mod;
    if (t.l < l)
      t.v = (t.v - C(d, t.l)) % mod;
    if (t.r < r)
      t.v = (t.v + C(d, r)) % mod;
    t.d = d;
    t.l = l;
    t.r = r;
  }
  return t;
}

int gao(int n, int m) {
  if (n > m)
    swap(n, m);
  // cout<<n<<" "<<m<<endl;
  int ans = 0;
  nd t;
  t.d = t.l = t.r = 0;
  t.v = 1;
  rep(i, 0, m + n) {
    int l = max(i - m, 0ll), r = min(i, n);
    t = cal(t, i, l, r);
    ans = (ans + t.v) % mod;
    //	cout<<t.d<<" "<<t.v<<" "<<t.l<<" "<<t.r<<endl;
  }
  return ans;
}
signed main() {
  fac[0] = inv[0] = 1;
  rep(i, 1, 3e6 - 1) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = ksm(fac[i], mod - 2);
  }
  // cout<<C(0,0);
  /*
{	nd t;
  t.d = t.l = t.r = 0;
  t.v = 1;
  rep(i,1,20){
          t=cal(t,i,0,i);
          cout<<t.v<<endl;
  }
}
*/
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int t = gao(r2, c2) - gao(r1 - 1, c2) - gao(r2, c1 - 1) + gao(c1 - 1, r1 - 1);
  t = (t % mod + mod) % mod;
  cout << t << endl;
  getchar();
  getchar();
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define int long long
const int mod = 1e9 + 7;
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
struct nd {
  int l, r;
  int d;
  int v;
};
int fac[3000000], inv[3000000];
int C(int n, int m) {
  if (n < 0 || m > n)
    return 0;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
nd cal(nd t, int d, int l, int r) {
  // nd res = t;
  if (r - l + 1 <= 4) {
    rep(i, t.l, t.r) t.v = (t.v - C(t.d, i)) % mod;
    t.d = d;
    t.l = l;
    t.r = r;
    rep(i, l, r) t.v = (t.v + C(t.d, i)) % mod;
  } else {
    t.v = (2 * t.v % mod - C(t.d, t.l) - C(t.d, t.r)) % mod;
    t.v = (t.v + C(d, t.l)) % mod;
    if (t.l < l)
      t.v = (t.v - C(d, t.l)) % mod;
    if (t.r < r)
      t.v = (t.v + C(d, r)) % mod;
    t.d = d;
    t.l = l;
    t.r = r;
  }
  return t;
}

int gao(int n, int m) {
  if (n > m)
    swap(n, m);
  // cout<<n<<" "<<m<<endl;
  int ans = 0;
  nd t;
  t.d = t.l = t.r = 0;
  t.v = 1;
  rep(i, 0, m + n) {
    int l = max(i - m, 0ll), r = min(i, n);
    t = cal(t, i, l, r);
    ans = (ans + t.v) % mod;
    //	cout<<t.d<<" "<<t.v<<" "<<t.l<<" "<<t.r<<endl;
  }
  return ans;
}
signed main() {
  fac[0] = inv[0] = 1;
  rep(i, 1, 3e6 - 1) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = ksm(fac[i], mod - 2);
  }
  // cout<<C(0,0);
  /*
{	nd t;
  t.d = t.l = t.r = 0;
  t.v = 1;
  rep(i,1,20){
          t=cal(t,i,0,i);
          cout<<t.v<<endl;
  }
}
*/
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int t = gao(r2, c2) - gao(r1 - 1, c2) - gao(r2, c1 - 1) + gao(c1 - 1, r1 - 1);
  t = (t % mod + mod) % mod;
  cout << t << endl;
  getchar();
  getchar();
}