Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;
const int mod = 998244353;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
  mint operator()(int n) { return fact[n]; }
};

int main() {
  lint N, M, K;
  cin >> N >> M >> K;
  combination comb(10000000);
  mint ans = 0;
  for (int k = 0; k <= K; k++) {
    int l = (N - 1) - k;
    // dd(k, l);
    // d(comb(M, l + 1).x);
    ans += comb(N - 1, l) * comb(M, 1) * comb(M - 1, 1).pow(l);
    // cout << ans.x << endl;
  }
  cout << ans.x << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;
const int mod = 998244353;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
  mint operator()(int n) { return fact[n]; }
};

int main() {
  lint N, M, K;
  cin >> N >> M >> K;
  combination comb(10000000);
  mint ans = 0;
  for (int k = 0; k <= K; k++) {
    int l = (N - 1) - k;
    // dd(k, l);
    // d(comb(M, l + 1).x);
    ans += comb(N - 1, l) * comb(M, 1) * comb(M - 1, 1).pow(N - 2);
    // cout << ans.x << endl;
  }
  cout << ans.x << endl;
  return 0;
}