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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  init();
  ll N, K;
  cin >> N >> K;
  vl A(N);

  ll m = 0, p = 0, z = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] == 0)
      z++;
    else if (A[i] < 0)
      m++;
    else
      p++;
  }

  sort(all(A), [&](ll a, ll b) { return abs(a) > abs(b); });

  if (N == K) {
    ll ret = 1;
    rep(i, N)(ret *= A[i]) %= MOD;
    cout << (ret + MOD) % MOD << endl;
    return 0;
  } else if (K + z > N or (z > 0 and p == 0 and K % 2)) {
    cout << 0 << endl;
    return 0;
  } else if (p == 0 and K % 2) {
    ll ret = 1;
    repe(i, N - K, N)(ret *= A[i]) %= MOD;
    cout << (ret + MOD) % MOD << endl;
    return 0;
  }

  ll ans = 1, mcnt = 0;
  rep(i, K) {
    (ans *= A[i]) %= MOD;
    if (A[i] < 0)
      mcnt++;
  }
  ans = (ans + MOD) % MOD;

  if (ans == 0) {
    cout << 0 << endl;
    return 0;
  } else if (mcnt % 2 == 0) {
    cout << ans << endl;
    return 0;
  }

  ll pi = -1, mi = -1;
  repr(i, K) {
    if (mi == -1 and A[i] < 0)
      mi = i;
    if (pi == -1 and A[i] >= 0)
      pi = i;
  }

  ll pj = -1, mj = -1;
  repe(i, K, N) {
    if (mj == -1 and A[i] < 0)
      mj = i;
    if (pj == -1 and A[i] >= 0)
      pj = i;
  }

  if (pi != -1 and pj != -1 and mi != -1 and mj != -1) {
    if (A[pi] * A[pj] > A[mi] * A[mj]) {
      ll mm = modpow(A[mi] + MOD, MOD - 2, MOD);
      (ans *= mm * A[pj] % MOD) %= MOD;
    } else {
      ll pp = modpow(A[pi], MOD - 2, MOD);
      (ans *= pp * (A[mj] + MOD) % MOD) %= MOD;
    }
  } else if (pi != -1 and mj != -1) {
    ll pp = modpow(A[pi], MOD - 2, MOD);
    (ans *= pp * (A[mj] + MOD) % MOD) %= MOD;
  } else if (pj != -1 and mi != -1) {
    ll mm = modpow(A[mi] + MOD, MOD - 2, MOD);
    (ans *= mm * A[pj] % MOD) %= MOD;
  } else {
    ans = 1;
    repe(i, N - K, N) { (ans *= A[i]) %= MOD; }
    (ans += MOD) %= MOD;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  init();
  ll N, K;
  cin >> N >> K;
  vl A(N);

  ll m = 0, p = 0, z = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] == 0)
      z++;
    else if (A[i] < 0)
      m++;
    else
      p++;
  }

  if (N == K) {
    ll ret = 1;
    rep(i, N)(ret *= A[i]) %= MOD;
    cout << (ret + MOD) % MOD << endl;
    return 0;
  } else if (K + z > N or (z > 0 and p == 0 and K % 2)) {
    cout << 0 << endl;
    return 0;
  } else if (p == 0 and K % 2) {
    ll ret = 1;
    rep(i, K)(ret *= A[i]) %= MOD;
    cout << (ret + MOD) % MOD << endl;
    return 0;
  }

  sort(all(A), [&](ll a, ll b) { return abs(a) > abs(b); });

  ll ans = 1, mcnt = 0;
  rep(i, K) {
    (ans *= A[i]) %= MOD;
    if (A[i] < 0)
      mcnt++;
  }

  if (ans == 0) {
    cout << 0 << endl;
    return 0;
  } else if (mcnt % 2 == 0) {
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }

  ll pi = -1, mi = -1;
  repr(i, K) {
    if (mi == -1 and A[i] < 0)
      mi = i;
    if (pi == -1 and A[i] > 0)
      pi = i;
  }

  ll pj = -1, mj = -1;
  repe(i, K, N) {
    if (mj == -1 and A[i] < 0)
      mj = i;
    if (pj == -1 and A[i] > 0)
      pj = i;
  }
  assert(pj != -1 and mj != -1);

  if (A[pi] * A[pj] > A[mi] * A[mj]) {
    ll mm = modpow(A[mi] + MOD, MOD - 2, MOD);
    (ans *= mm * A[pj] % MOD) %= MOD;
  } else {
    ll pp = modpow(A[pi], MOD - 2, MOD);
    (ans *= pp * (A[mj] + MOD) % MOD) %= MOD;
  }

  cout << ans << endl;
}