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

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

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

#define BASEP 998244353

ll powmod(ll x, ll pow, ll mod) {
  ll result = 1;
  while (pow > 0) {
    if (pow & 1) {
      result *= x;
      result %= mod;
    }

    x *= x;
    x %= mod;

    pow >>= 1;
  }
  return result;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  ll Ndash = N - 1;
  ll Mdash = M - 1;

  // 累乗を求めておく
  vector<ll> vpow(Ndash + 1);
  vpow[0] = 1;
  for (ll i = 0; i < Ndash; i++) {
    vpow[i + 1] = (vpow[i] * Mdash) % BASEP;
  }

  // 階乗を求めておく
  // vector<ll> vfact(Ndash + 1);
  // //ここvectorだとなぜかデストラクタ？で例外が起きる
  ll vfact[Ndash + 1];
  vfact[0] = 1;
  for (ll i = 0; i <= Ndash;) {
    ll back = vfact[i];
    i++;
    vfact[i] = (back * i) % BASEP;
  }

  // 階乗の逆元を求めておく
  vector<ll> vinv(Ndash + 1);
  for (ll i = 0; i <= Ndash; i++) {
    vinv[i] = powmod(vfact[i], BASEP - 2, BASEP);
  }

  // 組み合わせnCkを求めておく
  vector<ll> vnck(Ndash + 1);
  vnck[0] = vnck[Ndash] = 1;
  for (ll i = 1; i <= Ndash / 2; i++) {
    ll result = (vfact[Ndash] * vinv[i]) % BASEP;
    result = (result * vinv[Ndash - i]) % BASEP;
    vnck[i] = vnck[Ndash - i] = result;
  }

  // 目的の数式を計算する
  ll result = 0;
  for (ll i = 0; i <= K; i++) {
    result += (vnck[i] * vpow[Ndash - i]) % BASEP;
    result %= BASEP;
  }
  result *= M;
  result %= BASEP;
  OUTX(result);

  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

#define BASEP 998244353

ll powmod(ll x, ll pow, ll mod) {
  ll result = 1;
  while (pow > 0) {
    if (pow & 1) {
      result *= x;
      result %= mod;
    }

    x *= x;
    x %= mod;

    pow >>= 1;
  }
  return result;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  ll Ndash = N - 1;
  ll Mdash = M - 1;

  // 累乗を求めておく
  vector<ll> vpow(Ndash + 1);
  vpow[0] = 1;
  for (ll i = 0; i < Ndash; i++) {
    vpow[i + 1] = (vpow[i] * Mdash) % BASEP;
  }

  // 階乗を求めておく
  vector<ll> vfact(Ndash + 1);
  vfact[0] = 1;
  for (ll i = 0; i <= Ndash;) {
    ll back = vfact[i];
    i++;
    vfact[i] = (back * i) % BASEP;
  }

  // 階乗の逆元を求めておく
  vector<ll> vinv(Ndash + 1);
  for (ll i = 0; i <= Ndash; i++) {
    vinv[i] = powmod(vfact[i], BASEP - 2, BASEP);
  }

  // 組み合わせnCkを求めておく
  vector<ll> vnck(Ndash + 1);
  vnck[0] = vnck[Ndash] = 1;
  for (ll i = 1; i <= Ndash / 2; i++) {
    ll result = (vfact[Ndash] * vinv[i]) % BASEP;
    result = (result * vinv[Ndash - i]) % BASEP;
    vnck[i] = vnck[Ndash - i] = result;
  }

  // 目的の数式を計算する
  ll result = 0;
  for (ll i = 0; i <= K; i++) {
    result += (vnck[i] * vpow[Ndash - i]) % BASEP;
    result %= BASEP;
  }
  result *= M;
  result %= BASEP;
  OUTX(result);

  return 0;
}