Problem p04040 (C++)

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define sd(mark) scanf("%d", &mark)
#define ss(mark) scanf("%s", &mark)
#define slld(mark) scanf("%lld", &mark)
#define clr(mark) memset(mark, 0, sizeof(mark))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define sz(x) (int((x).size()))
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLL pair<long long, long long>
#define PIS pair<int, string>
#define MII map<int, int>
#define LL long long
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= (a); i--)
#define FILEIO(name)                                                           \
  freopen(name ".in", "r", stdin);                                             \
  freopen(name ".out", "w", stdout);
#define INF 2000000000              // 2 * 10^9
#define INFLL 1000000000000000000LL // 10^18
#define mod 1000000007

#define N 112345

LL fact[N], ifact[N];

LL power(LL a, LL b) {
  LL rs = 1, cr = a;
  while (b) {
    if (b & 1)
      rs = (rs * cr) % mod;
    cr = (cr * cr) % mod;
    b >>= 1;
  }
  return rs;
}

LL ncr(int n, int r) {
  LL rs = fact[n];
  rs *= ifact[r];
  rs %= mod;
  rs *= ifact[n - r];
  return rs % mod;
}

void solve() {
  int h, w, a, b, i;
  sd(h);
  sd(w);
  sd(a);
  sd(b);
  fact[0] = 1;
  for (i = 1; i < N; ++i)
    fact[i] = (fact[i - 1] * i) % mod;
  ifact[N - 1] = power(fact[N - 1], mod - 2);
  for (i = N - 2; i >= 0; --i)
    ifact[i] = (ifact[i + 1] * (i + 1)) % mod;
  LL rs = 0;
  // cout << ifact[0] << endl;
  for (i = 0; i < (h - a); ++i) {
    rs += (ncr(b - 1 + i, i) * ncr(w - 1 - b + h - 1 - i, h - 1 - i));
    rs %= mod;
  }
  printf("%lld\n", rs);
}

int main() {
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define sd(mark) scanf("%d", &mark)
#define ss(mark) scanf("%s", &mark)
#define slld(mark) scanf("%lld", &mark)
#define clr(mark) memset(mark, 0, sizeof(mark))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define sz(x) (int((x).size()))
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLL pair<long long, long long>
#define PIS pair<int, string>
#define MII map<int, int>
#define LL long long
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= (a); i--)
#define FILEIO(name)                                                           \
  freopen(name ".in", "r", stdin);                                             \
  freopen(name ".out", "w", stdout);
#define INF 2000000000              // 2 * 10^9
#define INFLL 1000000000000000000LL // 10^18
#define mod 1000000007

#define N 212345

LL fact[N], ifact[N];

LL power(LL a, LL b) {
  LL rs = 1, cr = a;
  while (b) {
    if (b & 1)
      rs = (rs * cr) % mod;
    cr = (cr * cr) % mod;
    b >>= 1;
  }
  return rs;
}

LL ncr(int n, int r) {
  LL rs = fact[n];
  rs *= ifact[r];
  rs %= mod;
  rs *= ifact[n - r];
  return rs % mod;
}

void solve() {
  int h, w, a, b, i;
  sd(h);
  sd(w);
  sd(a);
  sd(b);
  fact[0] = 1;
  for (i = 1; i < N; ++i)
    fact[i] = (fact[i - 1] * i) % mod;
  ifact[N - 1] = power(fact[N - 1], mod - 2);
  for (i = N - 2; i >= 0; --i)
    ifact[i] = (ifact[i + 1] * (i + 1)) % mod;
  LL rs = 0;
  // cout << ifact[0] << endl;
  for (i = 0; i < (h - a); ++i) {
    rs += (ncr(b - 1 + i, i) * ncr(w - 1 - b + h - 1 - i, h - 1 - i));
    rs %= mod;
  }
  printf("%lld\n", rs);
}

int main() {
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
}