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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

template <int M> class ModInt {
  int x;

public:
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y) : x(y >= 0 ? y % M : (M - (-y) % M) % M) {}
  constexpr ModInt &operator+=(const ModInt p) {
    if ((x += p.x) >= M)
      x -= M;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt p) {
    if ((x += M - p.x) >= M)
      x -= M;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt p) {
    x = (int)(1LL * x * p.x % M);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt p) {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt p) const {
    return ModInt(*this) += p;
  }
  constexpr ModInt operator-(const ModInt p) const {
    return ModInt(*this) -= p;
  }
  constexpr ModInt operator*(const ModInt p) const {
    return ModInt(*this) *= p;
  }
  constexpr ModInt operator/(const ModInt p) const {
    return ModInt(*this) /= p;
  }
  constexpr bool operator==(const ModInt p) const { return x == p.x; }
  constexpr bool operator!=(const ModInt p) const { return x != p.x; }
  constexpr ModInt inverse() const {
    int a = x, b = M, u = 1, v = 0, t = 0;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t k) const {
    ModInt ret(1), mul(x);
    while (k > 0) {
      if (k & 1)
        ret *= mul;
      mul *= mul;
      k >>= 1;
    }
    return ret;
  }
  constexpr friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
  constexpr friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t = 0;
    is >> t;
    a = ModInt(t);
    return (is);
  }
};
using mint = ModInt<mod>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  if (k == n) {
    mint ans(1);
    rep(i, n) {
      int x;
      cin >> x;
      ans *= x;
    }
    cout << ans << endl;
    return 0;
  }

  vector<ll> p, m;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x < 0)
      m.push_back(x);
    else
      p.push_back(x);
  }

  if (m.size() == n) {
    sort(ALL(m));
    mint ans = 1;
    if ((k & 1) == 0)
      rep(i, k) ans *= m[i];
    else
      rep(i, k) ans *= m[n - 1 - i];
    cout << ans << endl;
    return 0;
  }

  vector<ll> q1;
  vector<ll> q2;

  sort(ALL(p), greater<ll>());
  sort(ALL(m));

  mint ans = 1;
  int idx = 0;
  for (; idx + 1 < m.size() && idx + 2 <= k; idx += 2) {
    ans *= m[idx] * m[idx + 1];
    q1.push_back(m[idx]);
    q1.push_back(m[idx + 1]);
  }
  rep(i, p.size()) {
    if (idx++ < k) {
      ans *= p[i];
    } else
      q2.push_back(p[i]);
  }

  sort(ALL(q1), greater<ll>());
  sort(ALL(q2), greater<ll>());

  for (int i = 0; i + 1 < q1.size() && i + 1 < q2.size(); i += 2) {
    if (q1[i] * q1[i + 1] < q2[i] * q2[i + 1]) {
      ans /= q1[i] * q1[i + 1];
      ans *= q2[i] * q2[i + 1];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

template <int M> class ModInt {
  int x;

public:
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y) : x(y >= 0 ? y % M : (M - (-y) % M) % M) {}
  constexpr ModInt &operator+=(const ModInt p) {
    if ((x += p.x) >= M)
      x -= M;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt p) {
    if ((x += M - p.x) >= M)
      x -= M;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt p) {
    x = (int)(1LL * x * p.x % M);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt p) {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt p) const {
    return ModInt(*this) += p;
  }
  constexpr ModInt operator-(const ModInt p) const {
    return ModInt(*this) -= p;
  }
  constexpr ModInt operator*(const ModInt p) const {
    return ModInt(*this) *= p;
  }
  constexpr ModInt operator/(const ModInt p) const {
    return ModInt(*this) /= p;
  }
  constexpr bool operator==(const ModInt p) const { return x == p.x; }
  constexpr bool operator!=(const ModInt p) const { return x != p.x; }
  constexpr ModInt inverse() const {
    int a = x, b = M, u = 1, v = 0, t = 0;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t k) const {
    ModInt ret(1), mul(x);
    while (k > 0) {
      if (k & 1)
        ret *= mul;
      mul *= mul;
      k >>= 1;
    }
    return ret;
  }
  constexpr friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
  constexpr friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t = 0;
    is >> t;
    a = ModInt(t);
    return (is);
  }
};
using mint = ModInt<mod>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  if (k == n) {
    mint ans(1);
    rep(i, n) {
      int x;
      cin >> x;
      ans *= x;
    }
    cout << ans << endl;
    return 0;
  }

  vector<ll> p, m;
  int cnt0 = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x == 0)
      cnt0++;
    else if (x < 0)
      m.push_back(x);
    else
      p.push_back(x);
  }

  if (m.size() == n) {
    sort(ALL(m));
    mint ans = 1;
    if ((k & 1) == 0)
      rep(i, k) ans *= m[i];
    else
      rep(i, k) ans *= m[n - 1 - i];
    cout << ans << endl;
    return 0;
  }

  if (n - cnt0 < k) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> q1;
  vector<ll> q2;

  sort(ALL(p), greater<ll>());
  sort(ALL(m));

  mint ans = 1;
  int idx = 0;
  if (k & 1)
    ans *= p[idx++];
  for (; idx + 1 < p.size(); idx += 2) {
    ans *= p[idx];
    ans *= p[idx + 1];
    q1.push_back(p[idx]);
    q1.push_back(p[idx + 1]);
  }

  rep(i, m.size()) {
    if (i < k - idx)
      ans *= m[i];
    else
      q2.push_back(m[i]);
  }

  sort(ALL(q1));
  sort(ALL(q2));

  idx = 0;
  while (idx + 1 < min(q1.size(), q2.size())) {
    if (q1[idx] * q1[idx + 1] >= q2[idx] * q2[idx + 1])
      break;
    ans /= q1[idx] * q1[idx + 1];
    ans *= q2[idx] * q2[idx + 1];
    idx += 2;
  }

  cout << ans << endl;

  return 0;
}