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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define pb push_back
#define mod 1000000007
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int k, n;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> pos, neg, zero;
  vector<ll> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg.pb(a[i]);
    else if (a[i] > 0)
      pos.pb(a[i]);
    else
      zero.pb(a[i]);
    b[i] = abs(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  reverse(neg.begin(), neg.end());
  sort(b.begin(), b.end());
  int pp = pos.size(), nn = neg.size(), zz = zero.size();
  if ((nn == n && (k & 1)) || (k == n && (nn & 1))) {
    if (zz) {
      cout << "0\\n";
      return 0;
    }
    ll ans = 1;
    for (int i = 0; i < k; i++)
      ans = ans * b[i] % mod;
    ans = (mod - ans) % mod;
    cout << ans << "\\n";
  } else {
    ll ans = 1;
    if (k & 1) {
      if (!pp && zz) {
        cout << "0\\n";
        return 0;
      }
      ans = pos.back();
      pos.pop_back();
      k--;
    }
    while (k) {
      ll P = (pos.size() >= 2) ? (pos.back() * pos[pos.size() - 2]) : 0;
      ll N = (neg.size() >= 2) ? (neg.back() * neg[neg.size() - 2]) : 0;
      if (P > N) {
        ans = P % mod * ans % mod;
        if (P) {
          pos.pop_back();
          pos.pop_back();
        }
      } else {
        ans = N % mod * ans % mod;
        if (N) {
          neg.pop_back();
          neg.pop_back();
        }
      }
      k -= 2;
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define pb push_back
#define mod 1000000007
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int k, n;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> pos, neg, zero;
  vector<ll> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg.pb(a[i]);
    else if (a[i] > 0)
      pos.pb(a[i]);
    else
      zero.pb(a[i]);
    b[i] = abs(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  reverse(neg.begin(), neg.end());
  sort(b.begin(), b.end());
  int pp = pos.size(), nn = neg.size(), zz = zero.size();
  if (nn == n && (k & 1)) {
    ll ans = 1;
    for (int i = 0; i < k; i++)
      ans = ans * b[i] % mod;
    ans = (mod - ans) % mod;
    cout << ans << "\\n";
  } else {
    ll ans = 1;
    if (k & 1) {
      if (!pp && zz) {
        cout << "0\\n";
        return 0;
      }
      ans = pos.back();
      pos.pop_back();
      k--;
    }
    while (k) {
      ll P = (pos.size() >= 2) ? (pos.back() * pos[pos.size() - 2]) : 0;
      ll N = (neg.size() >= 2) ? (neg.back() * neg[neg.size() - 2]) : 0;
      if (P > N) {
        ans = P % mod * ans % mod;
        if (P)
          pos.pop_back();
        pos.pop_back();
      } else {
        ans = N % mod * ans % mod;
        if (N)
          neg.pop_back();
        neg.pop_back();
      }
      k -= 2;
    }
    cout << ans << "\\n";
  }
  return 0;
}