Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  const int MOD = 1e9 + 7;

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

  vll pos;
  vll neg;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a >= 0)
      pos.pb(a);
    else
      neg.pb(-a);
  }

  int po = pos.size();
  int ne = neg.size();

  if (n == k) {
    ll ans = 1;
    for (auto a : pos)
      ans = (ans * a) % MOD;
    for (auto a : neg)
      ans = (ans * a) % MOD;
    if (ne & 1)
      ans = (MOD - ans) % MOD;
    cout << ans << endl;
    return;
  }

  if (po == 0 && (k & 1)) {
    sort(all(neg));
    ll ans = 1;
    rep(i, k) { ans = (ans * neg[i]) % MOD; }
    ans = (MOD - ans) % MOD;
    cout << ans << endl;
    return;
  }

  sort(rall(pos));
  sort(rall(neg));

  ll ans = 1;
  int pi = 0;
  int ni = 0;
  ll tp, tn;

  if (k) {
    ans = (ans * pos[pi]) % MOD;
    k--;
    pi++;
  }

  while (k >= 2) {
    if (pi + 1 < po)
      tp = pos[pi] * pos[pi + 1];
    else
      tp = -1;
    if (ni + 1 < ne)
      tn = neg[ni] * neg[ni + 1];
    else
      tn = -1;
    if (tp == -1 || tn >= tp) {
      ans = (ans % MOD) * (tn % MOD) % MOD;
      ni += 2;
    } else {
      ans = (ans % MOD) * (tp % MOD) % MOD;
      pi += 2;
    }
    k -= 2;
  }

  // cout << ans << endl;
  // cout << k << ": " << ni << " " << pi << " " << endl;

  cout << ans << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  const int MOD = 1e9 + 7;

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

  vll pos;
  vll neg;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a >= 0)
      pos.pb(a);
    else
      neg.pb(-a);
  }

  int po = pos.size();
  int ne = neg.size();

  if (n == k) {
    ll ans = 1;
    for (auto a : pos)
      ans = (ans * a) % MOD;
    for (auto a : neg)
      ans = (ans * a) % MOD;
    if (ne & 1)
      ans = (MOD - ans) % MOD;
    cout << ans << endl;
    return;
  }

  if (po == 0 && (k & 1)) {
    sort(all(neg));
    ll ans = 1;
    rep(i, k) { ans = (ans * neg[i]) % MOD; }
    ans = (MOD - ans) % MOD;
    cout << ans << endl;
    return;
  }

  sort(rall(pos));
  sort(rall(neg));

  ll ans = 1;
  int pi = 0;
  int ni = 0;
  ll tp, tn;

  if (k & 1) {
    ans = (ans * pos[pi]) % MOD;
    k--;
    pi++;
  }

  while (k >= 2) {
    if (pi + 1 < po)
      tp = pos[pi] * pos[pi + 1];
    else
      tp = -1;
    if (ni + 1 < ne)
      tn = neg[ni] * neg[ni + 1];
    else
      tn = -1;
    if (tp == -1 || tn >= tp) {
      ans = (ans % MOD) * (tn % MOD) % MOD;
      ni += 2;
    } else {
      ans = (ans % MOD) * (tp % MOD) % MOD;
      pi += 2;
    }
    k -= 2;
  }

  // cout << ans << endl;
  // cout << k << ": " << ni << " " << pi << " " << endl;

  cout << ans << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}