Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
const ll INF = 1LL << 60;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll 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(ll 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;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> p, q, r;
  int zero = 0;
  for (ll A : a) {
    if (A > 0)
      p.push_back(A);
    if (A < 0)
      q.push_back(A);
    if (A == 0) {
      p.push_back(A);
      zero++;
    }
  }
  int MAX = min(k, (int)q.size()), MIN = max(k - (int)p.size(), 0);
  if (MAX != MIN || MIN % 2 == 0) { // ans is non-negative
    mint ans = 1;
    sort(rALL(p));
    sort(ALL(q));
    int ip = 0, iq = 0;
    while (k) {
      if (k >= 2 && (int)q.size() - iq >= 2 && (int)p.size() - ip >= 2 &&
          p[ip] * p[ip + 1] < q[iq] * q[iq + 1]) {
        k -= 2;
        ans *= q[iq];
        ans *= q[iq + 1];
        iq += 2;
      } else {
        k -= 1;
        ans *= p[ip];
        ip++;
      }
    }
    cout << ans.x << endl;
  } else if (zero == 0) { // ans is negative
    mint ans = 1;
    sort(ALL(p));
    sort(rALL(q));
    rep(i, min(k, (int)q.size())) { ans *= q[i]; }
    rep(i, k - (int)q.size()) { ans *= p[i]; }
    cout << ans.x << endl;
  } else { // ans is zero
    cout << 0 << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
const ll INF = 1LL << 60;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll 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(ll 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;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> p, q, r;
  int zero = 0;
  for (ll A : a) {
    if (A > 0)
      p.push_back(A);
    if (A < 0)
      q.push_back(A);
    if (A == 0) {
      p.push_back(A);
      zero++;
    }
  }
  int MAX = min(k, (int)q.size()), MIN = max(k - (int)p.size(), 0);
  if (MAX != MIN || MIN % 2 == 0) { // ans is non-negative
    mint ans = 1;
    sort(rALL(p));
    sort(ALL(q));
    int ip = 0, iq = 0;
    while (k) {
      if (k >= 2 && (int)q.size() - iq >= 2 && (int)p.size() - ip >= 2 &&
          p[ip] * p[ip + 1] < q[iq] * q[iq + 1]) {
        k -= 2;
        ans *= q[iq];
        ans *= q[iq + 1];
        iq += 2;
      } else if (k >= 2 && (int)q.size() - iq >= 2 && (int)p.size() - ip < 2) {
        k -= 2;
        ans *= q[iq];
        ans *= q[iq + 1];
        iq += 2;
      } else {
        k -= 1;
        ans *= p[ip];
        ip++;
      }
    }
    cout << ans.x << endl;
  } else if (zero == 0) { // ans is negative
    mint ans = 1;
    sort(ALL(p));
    sort(rALL(q));
    rep(i, min(k, (int)q.size())) { ans *= q[i]; }
    rep(i, k - (int)q.size()) { ans *= p[i]; }
    cout << ans.x << endl;
  } else { // ans is zero
    cout << 0 << endl;
  }

  return 0;
}