Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#define __mode_debug__
/*   ξ
     ll
   ＿ll＿
 ／  ∞  ＼
│*  Ａ  *│
│*  Ｃ  *│
│*  祈  *│
│*  願  *│
│*      *│
 ￣￣￣￣￣    */
// C++14 (GCC 5.4.1)
#include <bits/stdc++.h>
using namespace std;
#ifdef __mode_debug__
#define DBG
#else
#define DBG if (false)
#endif

#define LLINF (1LL << 60)
using ll = long long;
#define mod 1000000007

#define whole(a) (a).begin(), (a).end()
#define rwhole(a) (a).rbegin(), (a).rend()

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)
#define each(itr, ds) for (auto itr = (ds).begin(); itr != (ds).end(); itr++)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

// DUMP
//   ostream vector<T> , vector<vector<T>> , set<T> ,queue<T> ,
//   priority_queue<T> , pair<T,U> , map<T,U>
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{ ";
  rep(i, vec.size()) os << ((i == 0) ? "" : ", ") << i << ":" << vec[i];
  os << " }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  os << "{" << endl;
  rep(i, vec.size()) os << "\t   " << i << ":" << vec[i]
                        << ((i == vec.size() - 1) ? "" : ",") << endl;
  os << "\t }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &p) {
  os << "{ ";
  each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << *itr;
  os << " }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, queue<T> &p) {
  queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.front();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, priority_queue<T> &p) {
  priority_queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.top();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, map<T, U> &p) {
  os << "{ ";
  each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << itr->first << ":"
                  << itr->second;
  os << " }";
  return os;
}
void dump_f(string &nm, char t) { cerr << endl; }
template <class T, class... Ts>
void dump_f(string &nm, char t, T &&x, Ts &&...ts) {
  int l = nm.find_first_of(',');
  string name(nm, 0, l), nx(nm, l + 1, -1);
  if (t == '\n')
    cerr << "  " << name << "\t: " << x << t;
  else
    cerr << name << ": " << x << "," << t;
  dump_f(nx, t, forward<Ts>(ts)...);
}
template <class... Ts> void dump_m(int &&ln, string &&nm, char t, Ts &&...ts) {
  cerr << "[ln: " << ln << "]" << t;
  dump_f(nm, t, forward<Ts>(ts)...);
}
#define dump(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\n', __VA_ARGS__)
#define dl(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\t', __VA_ARGS__)

#define lower_bound_idx(V, c) distance(V.begin(), lower_bound(whole(V), c))

ll powmod(ll x, int n) {
  ll ans = 1, y = x;
  int k = n;
  while (k) {
    if (k & 1)
      ans = (ans * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return ans;
}

#define inv(x) powmod(x, mod - 2)
// mod付 階乗・階乗の逆元逆数を事前求値initF()
// 階乗をO(N)で求めた後、O(logMOD)で逆元を1度求値
// 1/x!*x = 1/(x-1)!を利用して戻るようにO(N)で逆元を一通り求める
#define needF 1000100
ll f[needF + 1], rf[needF + 1]; // f[x]=x!

void initF() {
  f[0] = 1;
  repd(i, 1, needF + 1) f[i] = (f[i - 1] * i) % mod;
  rf[needF] = inv(f[needF]);
  rrepd(i, needF - 1, 0) rf[i] = (rf[i + 1] * (i + 1)) % mod;
}
ll nPk(int n, int k) { return (f[n] * rf[n - k]) % mod; }
ll nCk(int n, int k) {
  ll bc = (rf[n - k] * rf[k]) % mod;
  return (f[n] * bc) % mod;
}

#define nHk(n, k) nCr((n) + (k)-1, k)
int main() {
  ios::sync_with_stdio(false); // stdout<=>cout
  cin.tie(nullptr);            // cin<=>cout

  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  initF();

  ll ans;
  ans = nCk(r2 + c2 + 2, r2 + 1) + nCk(r1 + c1, c1) - 2;
  // dl(ans);
  ans %= mod;
  ans -= nCk(r1 + c2 + 1, r1) + nCk(c1 + r2 + 1, c1) - 2;
  while (ans < 0)
    ans += mod;
  ans %= mod;

  cout << ans << endl;

  return (0);
}

Fixed submission:
#define __mode_debug__
/*   ξ
     ll
   ＿ll＿
 ／  ∞  ＼
│*  Ａ  *│
│*  Ｃ  *│
│*  祈  *│
│*  願  *│
│*      *│
 ￣￣￣￣￣    */
// C++14 (GCC 5.4.1)
#include <bits/stdc++.h>
using namespace std;
#ifdef __mode_debug__
#define DBG
#else
#define DBG if (false)
#endif

#define LLINF (1LL << 60)
using ll = long long;
#define mod 1000000007

#define whole(a) (a).begin(), (a).end()
#define rwhole(a) (a).rbegin(), (a).rend()

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)
#define each(itr, ds) for (auto itr = (ds).begin(); itr != (ds).end(); itr++)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

// DUMP
//   ostream vector<T> , vector<vector<T>> , set<T> ,queue<T> ,
//   priority_queue<T> , pair<T,U> , map<T,U>
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{ ";
  rep(i, vec.size()) os << ((i == 0) ? "" : ", ") << i << ":" << vec[i];
  os << " }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  os << "{" << endl;
  rep(i, vec.size()) os << "\t   " << i << ":" << vec[i]
                        << ((i == vec.size() - 1) ? "" : ",") << endl;
  os << "\t }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &p) {
  os << "{ ";
  each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << *itr;
  os << " }";
  return os;
}
template <class T> ostream &operator<<(ostream &os, queue<T> &p) {
  queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.front();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, priority_queue<T> &p) {
  priority_queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.top();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, map<T, U> &p) {
  os << "{ ";
  each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << itr->first << ":"
                  << itr->second;
  os << " }";
  return os;
}
void dump_f(string &nm, char t) { cerr << endl; }
template <class T, class... Ts>
void dump_f(string &nm, char t, T &&x, Ts &&...ts) {
  int l = nm.find_first_of(',');
  string name(nm, 0, l), nx(nm, l + 1, -1);
  if (t == '\n')
    cerr << "  " << name << "\t: " << x << t;
  else
    cerr << name << ": " << x << "," << t;
  dump_f(nx, t, forward<Ts>(ts)...);
}
template <class... Ts> void dump_m(int &&ln, string &&nm, char t, Ts &&...ts) {
  cerr << "[ln: " << ln << "]" << t;
  dump_f(nm, t, forward<Ts>(ts)...);
}
#define dump(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\n', __VA_ARGS__)
#define dl(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\t', __VA_ARGS__)

#define lower_bound_idx(V, c) distance(V.begin(), lower_bound(whole(V), c))

ll powmod(ll x, int n) {
  ll ans = 1, y = x;
  int k = n;
  while (k) {
    if (k & 1)
      ans = (ans * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return ans;
}

#define inv(x) powmod(x, mod - 2)
// mod付 階乗・階乗の逆元逆数を事前求値initF()
// 階乗をO(N)で求めた後、O(logMOD)で逆元を1度求値
// 1/x!*x = 1/(x-1)!を利用して戻るようにO(N)で逆元を一通り求める
#define needF 2000100
ll f[needF + 1], rf[needF + 1]; // f[x]=x!

void initF() {
  f[0] = 1;
  repd(i, 1, needF + 1) f[i] = (f[i - 1] * i) % mod;
  rf[needF] = inv(f[needF]);
  rrepd(i, needF - 1, 0) rf[i] = (rf[i + 1] * (i + 1)) % mod;
}
ll nPk(int n, int k) { return (f[n] * rf[n - k]) % mod; }
ll nCk(int n, int k) {
  ll bc = (rf[n - k] * rf[k]) % mod;
  return (f[n] * bc) % mod;
}

#define nHk(n, k) nCr((n) + (k)-1, k)
int main() {
  ios::sync_with_stdio(false); // stdout<=>cout
  cin.tie(nullptr);            // cin<=>cout

  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  initF();

  ll ans;
  ans = nCk(r2 + c2 + 2, r2 + 1) + nCk(r1 + c1, c1) - 2;
  // dl(ans);
  ans %= mod;
  ans -= nCk(r1 + c2 + 1, r1) + nCk(c1 + r2 + 1, c1) - 2;
  while (ans < 0)
    ans += mod;
  ans %= mod;

  cout << ans << endl;

  return (0);
}