Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F_OR(i, a, b, s)                                                       \
  for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, (b) < (e) ? 1 : -1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define FOR(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)(__VA_ARGS__)
#ifdef LOCAL
#define dbg(x) cerr << '[' << #x << ":" << (x) << "]\n"
#else
#define dbg(x)
#endif
const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  int npos = 0, nneg = 0;
  vector<int> pos, neg;
  FOR(n) {
    int a;
    cin >> a;
    if (a < 0)
      neg.pb(a), nneg++;
    else
      pos.pb(a), npos++;
  }
  sort(rall(pos));
  sort(all(neg));
  auto mul = [&](vector<int> &v, int x) {
    ll ans = 1;
    FOR(x)(ans *= v[i]) %= MOD;
    if (ans < 0)
      ans += MOD;
    return ans;
  };
  if (npos == 0) {
    if (k & 1)
      reverse(all(neg));
    cout << mul(neg, k);
    return 0;
  }
  int i = 0, j = 0;
  vector<int> chp, chn;
  FOR(z, k) {
    if (j == nneg || (i < npos && pos[i] > -neg[j]))
      chp.pb(pos[i]), i++;
    else
      chn.pb(neg[j]), j++;
  }
  if ((j & 1) ^ 1) {
    // dbg(i),dbg(j);
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
    return 0;
  }
  if (i < npos &&
      (j == nneg || (ll)chp.back() * pos[i] > (ll)chn.back() * neg[j])) {
    chp.pb(pos[i]);
    chn.pop_back();
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  } else if (j < nneg) {
    chn.pb(neg[j]);
    chp.pop_back();
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  } else {
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F_OR(i, a, b, s)                                                       \
  for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, (b) < (e) ? 1 : -1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define FOR(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)(__VA_ARGS__)
#ifdef LOCAL
#define dbg(x) cerr << '[' << #x << ":" << (x) << "]\n"
#else
#define dbg(x)
#endif
const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  int npos = 0, nneg = 0;
  vector<int> pos, neg;
  FOR(n) {
    int a;
    cin >> a;
    if (a < 0)
      neg.pb(a), nneg++;
    else
      pos.pb(a), npos++;
  }
  sort(rall(pos));
  sort(all(neg));
  auto mul = [&](vector<int> &v, int x) {
    ll ans = 1;
    FOR(x)(ans *= v[i]) %= MOD;
    if (ans < 0)
      ans += MOD;
    return ans;
  };
  if (npos == 0) {
    if (k & 1)
      reverse(all(neg));
    cout << mul(neg, k);
    return 0;
  }
  int i = 0, j = 0;
  vector<int> chp, chn;
  FOR(z, k) {
    if (j == nneg || (i < npos && pos[i] > -neg[j]))
      chp.pb(pos[i]), i++;
    else
      chn.pb(neg[j]), j++;
  }
  if ((j & 1) ^ 1) {
    // dbg(i),dbg(j);
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
    return 0;
  }
  if (i == 0 || i < npos && (j == nneg || (ll)chp.back() * pos[i] >
                                              (ll)chn.back() * neg[j])) {
    chp.pb(pos[i]);
    chn.pop_back();
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  } else if (j < nneg) {
    chn.pb(neg[j]);
    chp.pop_back();
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  } else {
    cout << mul(chp, chp.size()) * mul(chn, chn.size()) % MOD;
  }
}