Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << endl;
#define idbg(x) #x " = " << x
#include "pretty_print.h"
#else
#define dbg(x)
#define idbg(x) ""
#endif

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0 ? -x : x; }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

const int MOD = 1e+9 + 7;

vector<ll> f(1e+6 + 42);

ll powmod(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll r = powmod(a, b >> 1);
  (r *= r) %= MOD;
  return b & 1 ? a * r % MOD : r;
}

ll divmod(ll a, ll b) { return a * powmod(b, MOD - 2) % MOD; }

ll C(int n, int k) { return divmod(f[n], f[k] * f[n - k] % MOD); }

ll get(int r, int c) { return C(r + c, r); }

int main(int /* argc */, char ** /* argv */) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  assert(freopen("inp", "r", stdin));
  assert(freopen("out", "w", stdout));
#endif

  f[0] = 1;
  for (int i = 1; i < (int)f.size(); ++i) {
    f[i] = f[i - 1] * i % MOD;
  }

  int r1, c1, r2, c2;
  while (cin >> r1 >> c1 >> r2 >> c2) {
    ll cur = get(r1, c1);
    ll ans = cur;
    for (int i = 1; i <= (r2 + c2) - (r1 + c1); ++i) {
      (cur *= 2) %= MOD;
      if (r1 + i <= r2) {
        cur += get(r1 + i, c1 - 1);
      } else {
        cur -= get(r2, c1 + i - (r2 - r1 + 1));
      }

      if (c1 + i <= c2) {
        cur += get(r1 - 1, c1 + i);
      } else {
        cur -= get(r1 + i - (c2 - c1 + 1), c2);
      }
      cur %= MOD;
      (ans += cur) %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
  }

  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << endl;
#define idbg(x) #x " = " << x
#include "pretty_print.h"
#else
#define dbg(x)
#define idbg(x) ""
#endif

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0 ? -x : x; }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

const int MOD = 1e+9 + 7;

vector<ll> f(2e+6 + 42);

ll powmod(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll r = powmod(a, b >> 1);
  (r *= r) %= MOD;
  return b & 1 ? a * r % MOD : r;
}

ll divmod(ll a, ll b) { return a * powmod(b, MOD - 2) % MOD; }

ll C(int n, int k) { return divmod(f[n], f[k] * f[n - k] % MOD); }

ll get(int r, int c) { return C(r + c, r); }

int main(int /* argc */, char ** /* argv */) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  assert(freopen("inp", "r", stdin));
  assert(freopen("out", "w", stdout));
#endif

  f[0] = 1;
  for (int i = 1; i < (int)f.size(); ++i) {
    f[i] = f[i - 1] * i % MOD;
  }

  int r1, c1, r2, c2;
  while (cin >> r1 >> c1 >> r2 >> c2) {
    ll cur = get(r1, c1);
    ll ans = cur;
    for (int i = 1; i <= (r2 + c2) - (r1 + c1); ++i) {
      (cur *= 2) %= MOD;
      if (r1 + i <= r2) {
        cur += get(r1 + i, c1 - 1);
      } else {
        cur -= get(r2, c1 + i - (r2 - r1 + 1));
      }

      if (c1 + i <= c2) {
        cur += get(r1 - 1, c1 + i);
      } else {
        cur -= get(r1 + i - (c2 - c1 + 1), c2);
      }
      cur %= MOD;
      (ans += cur) %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
  }

  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}