Problem p02616 (C++)

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

#ifdef LOCAL
#include "debug_print.hpp"
#else
void debug(...) {}
#endif

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  ll x;
  ll cnt;
  mint(ll x = 0) : x((x % mod + mod) % mod), cnt(0) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint m) {
    if ((x += m.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint m) {
    if ((x += mod - m.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint m) {
    if (m.x == 0)
      cnt = 0;
    x *= m.x;
    if (x != 0)
      cnt += m.cnt;
    if (x >= mod) {
      cnt += x / mod;
      x %= mod;
    }
    return *this;
  }
  mint operator+(const mint m) const { return mint(*this) += m; }
  mint operator-(const mint m) const { return mint(*this) -= m; }
  mint operator*(const mint m) const { return mint(*this) *= m; }
  mint pow(ll n) const {
    if (!n)
      return 1;
    mint m = pow(n >> 1);
    m *= m;
    if (n & 1)
      m *= *this;
    return m;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint m) { return *this *= m.inv(); }
  mint operator/(const mint m) const { return mint(*this) /= m; }
};
istream &operator>>(istream &is, mint &m) { return is >> m.x; }
ostream &operator<<(ostream &os, const mint &m) { return os << m.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<ll> P, M;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      P.push_back(a);
    else
      M.push_back(-a);
  }
  if (N == K) {
    mint ans = 1;
    for (auto p : P)
      ans *= p;
    for (auto m : M)
      ans *= -m;
    cout << ans << endl;
    return 0;
  }
  if (M.size() == N && K % 2 == 1) {
    sort(M.begin(), M.end());
    mint ans = 1;
    for (int i = 0; i < K; i++)
      ans *= M[i];
    ans *= -1;
    cout << ans << endl;
    return 0;
  }

  sort(P.begin(), P.end(), greater<ll>());
  sort(M.begin(), M.end(), greater<ll>());
  int pidx = 0;
  int midx = 0;
  mint ans = 1;
  while (pidx + midx < K) {
    ll x = 0, y = 0;
    if (pidx + 1 < P.size())
      x = P[pidx] * P[pidx + 1];
    if (midx + 1 < M.size())
      y = M[midx] * M[midx + 1];
    if (x > y || pidx + midx == K - 1) {
      ans *= P[pidx];
      pidx++;
    } else {
      ans *= M[midx];
      ans *= M[midx + 1];
      midx += 2;
    }
  }
  cout << ans << endl;

  return 0;
}


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

#ifdef LOCAL
#include "debug_print.hpp"
#else
void debug(...) {}
#endif

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  ll x;
  ll cnt;
  mint(ll x = 0) : x((x % mod + mod) % mod), cnt(0) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint m) {
    if ((x += m.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint m) {
    if ((x += mod - m.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint m) {
    if (m.x == 0)
      cnt = 0;
    x *= m.x;
    if (x != 0)
      cnt += m.cnt;
    if (x >= mod) {
      cnt += x / mod;
      x %= mod;
    }
    return *this;
  }
  mint operator+(const mint m) const { return mint(*this) += m; }
  mint operator-(const mint m) const { return mint(*this) -= m; }
  mint operator*(const mint m) const { return mint(*this) *= m; }
  mint pow(ll n) const {
    if (!n)
      return 1;
    mint m = pow(n >> 1);
    m *= m;
    if (n & 1)
      m *= *this;
    return m;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint m) { return *this *= m.inv(); }
  mint operator/(const mint m) const { return mint(*this) /= m; }
};
istream &operator>>(istream &is, mint &m) { return is >> m.x; }
ostream &operator<<(ostream &os, const mint &m) { return os << m.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<ll> P, M;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      P.push_back(a);
    else
      M.push_back(-a);
  }
  if (N == K) {
    mint ans = 1;
    for (auto p : P)
      ans *= p;
    for (auto m : M)
      ans *= -m;
    cout << ans << endl;
    return 0;
  }
  if (M.size() == N && K % 2 == 1) {
    sort(M.begin(), M.end());
    mint ans = 1;
    for (int i = 0; i < K; i++)
      ans *= M[i];
    ans *= -1;
    cout << ans << endl;
    return 0;
  }

  sort(P.begin(), P.end(), greater<ll>());
  sort(M.begin(), M.end(), greater<ll>());
  int pidx = 0;
  int midx = 0;
  mint ans = 1;
  while (pidx + midx < K) {
    ll x = -1, y = -1;
    if (pidx + 1 < P.size())
      x = P[pidx] * P[pidx + 1];
    if (midx + 1 < M.size())
      y = M[midx] * M[midx + 1];
    if (x > y || pidx + midx == K - 1) {
      ans *= P[pidx];
      pidx++;
    } else {
      ans *= M[midx];
      ans *= M[midx + 1];
      midx += 2;
    }
  }
  cout << ans << endl;

  return 0;
}