Problem p02878 (C++)

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

using ll = long long;
ll modinv(ll a, ll m) {
  assert(m > 0);
  if (m == 1)
    return 0;
  a %= m;
  if (a < 0)
    a += m;
  assert(a != 0);
  if (a == 1)
    return 1;
  return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
  int v;

public:
  static const int MOD = MOD_;

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) {
    if (v < 0)
      v += MOD;
  }
  explicit operator int() const { return v; }
  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum operator~() const {
    modnum res;
    res.v = modinv(v, MOD);
    return res;
  }

  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MOD;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= (~o); }

  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }

  friend modnum pow() {}
};
using num = modnum<998244353>;

template <typename T> T pow(T a, long long b) {
  assert(b >= 0);
  T r = 1;
  while (b) {
    if (b & 1)
      r *= a;
    b >>= 1;
    a *= a;
  }
  return r;
}

vector<num> fact;
vector<num> ifact;

void init() {
  fact = {1};
  for (int i = 1; i < 1100000; i++)
    fact.push_back(i * fact[i - 1]);
  ifact.resize(fact.size());
  ifact.back() = 1 / fact.back();
  for (int i = (int)ifact.size() - 1; i > 0; i--)
    ifact[i - 1] = i * ifact[i];
}

num ncr(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * ifact[k] * ifact[n - k];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  int n, a, b;
  cin >> n >> a >> b;

  num ans = 0;
  for (int i = 0; i + b <= n; i++) {
    int nreset = i;
    int nup = n - b - nreset;
    int nright = b;
    num ways = 0;
    if (nright == 0 && nup == 0) {
      ways = 1;
    } else if (nright <= nup || b - a > nright - nup) {
      ways = 0;
    } else {
      num path = ncr(nright + nup - 1, nup) / nright * (nright - nup);
      int z = (nright - nup) - (b - a);
      if (z > 0) {
        if (nreset == 0)
          continue;
        nreset -= 1;
      }
      num mult = ncr(z + nreset, nreset);
      ways = path * mult;
    }
    ans += ways;
  }
  cout << (int)ans << '\n';
}

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

using ll = long long;
ll modinv(ll a, ll m) {
  assert(m > 0);
  if (m == 1)
    return 0;
  a %= m;
  if (a < 0)
    a += m;
  assert(a != 0);
  if (a == 1)
    return 1;
  return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
  int v;

public:
  static const int MOD = MOD_;

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) {
    if (v < 0)
      v += MOD;
  }
  explicit operator int() const { return v; }
  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum operator~() const {
    modnum res;
    res.v = modinv(v, MOD);
    return res;
  }

  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MOD;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= (~o); }

  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }

  friend modnum pow() {}
};
using num = modnum<998244353>;

template <typename T> T pow(T a, long long b) {
  assert(b >= 0);
  T r = 1;
  while (b) {
    if (b & 1)
      r *= a;
    b >>= 1;
    a *= a;
  }
  return r;
}

vector<num> fact;
vector<num> ifact;

void init() {
  fact = {1};
  for (int i = 1; i < 21000000; i++)
    fact.push_back(i * fact[i - 1]);
  ifact.resize(fact.size());
  ifact.back() = 1 / fact.back();
  for (int i = (int)ifact.size() - 1; i > 0; i--)
    ifact[i - 1] = i * ifact[i];
}

num ncr(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * ifact[k] * ifact[n - k];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  int n, a, b;
  cin >> n >> a >> b;

  num ans = 0;
  for (int i = 0; i + b <= n; i++) {
    int nreset = i;
    int nup = n - b - nreset;
    int nright = b;
    num ways = 0;
    if (nright == 0 && nup == 0) {
      ways = 1;
    } else if (nright <= nup || b - a > nright - nup) {
      ways = 0;
    } else {
      num path = ncr(nright + nup - 1, nup) / nright * (nright - nup);
      int z = (nright - nup) - (b - a);
      if (z > 0) {
        if (nreset == 0)
          continue;
        nreset -= 1;
      }
      num mult = ncr(z + nreset, nreset);
      ways = path * mult;
    }
    ans += ways;
  }
  cout << (int)ans << '\n';
}