Problem p04046 (C++)

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

#define int long long
#define pb push_back
#define f first
#define s second
#define all(container) container.begin(), container.end()

#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
#define print(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << ": " << arg1 << '\n';
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int sz = 2e5 + 1;
const int mod = 1e9 + 7;
int fact[sz], ifact[sz];

int fastmod(int base, int exp) {

  if (exp == 0)
    return 1;
  else if (exp % 2 == 0) {
    int ans = fastmod(base, exp / 2);
    return (ans % mod * ans % mod) % mod;
  } else {
    return (fastmod(base, exp - 1) % mod * base % mod) % mod;
  }
}

void factifact() {

  fact[0] = 1;
  for (int i = 1; i < sz; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  ifact[sz - 1] = fastmod(fact[sz - 1], mod - 2);
  for (int i = sz - 2; i >= 0; i--)
    ifact[i] = (ifact[i + 1] * (i + 1)) % mod;
}

int nCr(int n, int r) {
  return (fact[n] * (ifact[n - r] % mod * ifact[r] % mod) % mod) % mod;
}

int32_t main() {

  fast;
  factifact();

  int h, w, a, b;
  cin >> h >> w >> a >> b;
  h--;
  w--;

  int cnt = 0;

  for (int i = h - a + 1; i <= (b + h - a); i++) {
    cnt += (nCr(b + h - a, i) % mod * nCr(w - b + a, h - i) % mod) % mod;
    cnt %= mod;
  }

  int ans = (nCr(w + h, h) % mod - cnt % mod + mod) % mod;

  cout << ans << '\n';

  return 0;
}


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

#define int long long
#define pb push_back
#define f first
#define s second
#define all(container) container.begin(), container.end()

#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
#define print(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << ": " << arg1 << '\n';
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int sz = 2e5 + 1;
const int mod = 1e9 + 7;
int fact[sz], ifact[sz];

int fastmod(int base, int exp) {

  if (exp == 0)
    return 1;
  else if (exp % 2 == 0) {
    int ans = fastmod(base, exp / 2);
    return (ans % mod * ans % mod) % mod;
  } else {
    return (fastmod(base, exp - 1) % mod * base % mod) % mod;
  }
}

void factifact() {

  fact[0] = 1;
  for (int i = 1; i < sz; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  ifact[sz - 1] = fastmod(fact[sz - 1], mod - 2);
  for (int i = sz - 2; i >= 0; i--)
    ifact[i] = (ifact[i + 1] * (i + 1)) % mod;
}

int nCr(int n, int r) {
  return (fact[n] * (ifact[n - r] % mod * ifact[r] % mod) % mod) % mod;
}

int32_t main() {

  fast;
  factifact();

  int h, w, a, b;
  cin >> h >> w >> a >> b;
  h--;
  w--;

  int cnt = 0;

  for (int i = h - a + 1; i <= min(b + h - a, h); i++) {
    cnt += (nCr(b + h - a, i) % mod * nCr(w - b + a, h - i) % mod) % mod;
    cnt %= mod;
  }

  int ans = (nCr(w + h, h) % mod - cnt % mod + mod) % mod;

  cout << ans << '\n';

  return 0;
}