Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define MOD 1'000'000'007LL
#define N 212345
ll fct[N] = {1};
ll fctinv[N];
ll pw(ll a, ll b) {
  ll c = 1;
  do {
    if (b & 1)
      c = c * a % MOD;
    a = a * a % MOD;
  } while (b /= 2);
  return c;
}
ll inv(ll a) { return pw(a, MOD - 2); }
ll cmb(ll a, ll b) { return fct[a] * fctinv[b] % MOD * fctinv[a - b] % MOD; }
ll g(ll a, ll b) {
  ll tmp = 0, sm = 0;
  REP(i, b) {
    tmp = tmp * 2 + cmb(a + i, i);
    tmp %= MOD;
    sm += tmp;
    sm %= MOD;
    DEBUG("  %d %lld %lld\n", i, tmp, sm);
  }
  DEBUG(" g(%lld, %lld) = %lld\n", a, b, sm);
  return sm;
}
ll f(ll a, ll b) {
  ll ret = (pw(2, a + b + 1) + MOD - 1) % MOD;
  DEBUG(" 2^() = %lld\n", ret);
  ret = (ret + MOD - g(a, b)) % MOD;
  ret = (ret + MOD - g(b, a)) % MOD;
  DEBUG("f(%lld, %lld) = %lld\n", a, b, ret);
  return ret;
}

int main() {
  FOR(i, 1, N) fct[i] = fct[i - 1] * i % MOD;
  REP(i, N) fctinv[i] = inv(fct[i]);
  ll a, b, c, d;
  scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
  a--;
  b--;
  ll ans = f(c, d) - f(a, d) - f(c, b) + f(a, b);
  ans = (ans + MOD * 3) % MOD;
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define MOD 1'000'000'007LL
#define N 2123456
ll fct[N] = {1};
ll fctinv[N];
ll pw(ll a, ll b) {
  ll c = 1;
  do {
    if (b & 1)
      c = c * a % MOD;
    a = a * a % MOD;
  } while (b /= 2);
  return c;
}
ll inv(ll a) { return pw(a, MOD - 2); }
ll cmb(ll a, ll b) { return fct[a] * fctinv[b] % MOD * fctinv[a - b] % MOD; }
ll g(ll a, ll b) {
  ll tmp = 0, sm = 0;
  REP(i, b) {
    tmp = tmp * 2 + cmb(a + i, i);
    tmp %= MOD;
    sm += tmp;
    sm %= MOD;
    DEBUG("  %d %lld %lld\n", i, tmp, sm);
  }
  DEBUG(" g(%lld, %lld) = %lld\n", a, b, sm);
  return sm;
}
ll f(ll a, ll b) {
  ll ret = (pw(2, a + b + 1) + MOD - 1) % MOD;
  DEBUG(" 2^() = %lld\n", ret);
  ret = (ret + MOD - g(a, b)) % MOD;
  ret = (ret + MOD - g(b, a)) % MOD;
  DEBUG("f(%lld, %lld) = %lld\n", a, b, ret);
  return ret;
}

int main() {
  FOR(i, 1, N) fct[i] = fct[i - 1] * i % MOD;
  REP(i, N) fctinv[i] = inv(fct[i]);
  ll a, b, c, d;
  scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
  a--;
  b--;
  ll ans = f(c, d) - f(a, d) - f(c, b) + f(a, b);
  ans = (ans + MOD * 3) % MOD;
  printf("%lld\n", ans);
}