Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define name ""
#define ini                                                                    \
  freopen(name ".inp", "r", stdin);                                            \
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 1e6 + 2;

long fact[N], ifact[N];

long POW(long x, long n) {
  if (n == 0)
    return 1;
  long y = POW(x, n / 2);
  y = y * y % MOD;
  if (n % 2)
    return x * y % MOD;
  else
    return y;
}

int main() {
  fastio;
  int u, v, x, y;
  cin >> u >> v >> x >> y;
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = fact[i - 1] * i % MOD;
  ifact[N - 1] = POW(fact[N - 1], MOD - 2);
  ifact[0] = 1;
  for (int i = N - 2; i > 0; i--)
    ifact[i] = ifact[i + 1] * (i + 1) % MOD;
  auto C = [&](int k, int n) {
    long res = fact[n] * (ifact[k] * ifact[n - k] % MOD) % MOD;
    return res;
  };
  auto F = [&](int u, int v) {
    long res = 0;
    for (int i = 1; i <= u + 1; i++) {
      res += C(i, v + i);
      res %= MOD;
    }
    return res;
  };
  long res = F(x, y) - F(u - 1, y) - F(x, v - 1) + F(u - 1, v - 1);
  res %= MOD;
  (res += MOD) %= MOD;
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define name ""
#define ini                                                                    \
  freopen(name ".inp", "r", stdin);                                            \
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 2e6 + 2;

long fact[N], ifact[N];

long POW(long x, long n) {
  if (n == 0)
    return 1;
  long y = POW(x, n / 2);
  y = y * y % MOD;
  if (n % 2)
    return x * y % MOD;
  else
    return y;
}

int main() {
  fastio;
  int u, v, x, y;
  cin >> u >> v >> x >> y;
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = fact[i - 1] * i % MOD;
  ifact[N - 1] = POW(fact[N - 1], MOD - 2);
  ifact[0] = 1;
  for (int i = N - 2; i > 0; i--)
    ifact[i] = ifact[i + 1] * (i + 1) % MOD;
  auto C = [&](int k, int n) {
    long res = fact[n] * (ifact[k] * ifact[n - k] % MOD) % MOD;
    return res;
  };
  auto F = [&](int u, int v) {
    long res = 0;
    for (int i = 1; i <= u + 1; i++) {
      res += C(i, v + i);
      res %= MOD;
    }
    return res;
  };
  long res = F(x, y) - F(u - 1, y) - F(x, v - 1) + F(u - 1, v - 1);
  res %= MOD;
  (res += MOD) %= MOD;
  cout << res;
  return 0;
}