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

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \\
  int __tc;                                                                    \\
  cin >> __tc;                                                                 \\
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))

const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
const int MAXN = 200005;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

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

  vector<ll> pos;
  vector<ll> neg;
  bool zero = false;

  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a < 0) {
      neg.pb(a);
    } else if (a > 0) {
      pos.pb(a);
    } else {
      zero = true;
    }
  }

  ll p = 1;

  if (2 * min(k / 2, SZ(neg) / 2) + SZ(pos) >= k) {
    // can be positive

    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end(), greater<ll>());

    if (k % 2 == 1) {
      p *= pos.back();
      pos.pop_back();
      p %= MOD;
      k--;
    }

    vector<ll> f;

    for (int i = SZ(neg) - 1; i >= 1; i -= 2) {
      f.pb((neg[i] * neg[i - 1]));
    }
    for (int i = SZ(pos) - 1; i >= 1; i -= 2) {
      f.pb((pos[i] * pos[i - 1]));
    }

    sort(f.begin(), f.end());

    while (k) {
      p *= f.back() % MOD;
      f.pop_back();
      p %= MOD;
      k -= 2;
    }

    cout << p << "\\n";
  } else if (zero) {
    cout << 0 << "\\n";
  } else {

    vector<ll> absolute;
    for (auto v : neg) {
      absolute.pb(llabs(v));
    }
    for (auto v : pos) {
      absolute.pb(llabs(v));
    }
    sort(absolute.begin(), absolute.end());

    for (int i = 0; i < k; i++) {
      p *= absolute[i];
      p %= MOD;
    }
    p = MOD - p;

    cout << p << "\\n";
  }
}


Error: 1

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \\
  int __tc;                                                                    \\
  cin >> __tc;                                                                 \\
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))

const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
const int MAXN = 200005;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

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

  vector<ll> pos;
  vector<ll> neg;
  bool zero = false;

  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a < 0) {
      neg.pb(a);
    } else if (a > 0) {
      pos.pb(a);
    } else {
      zero = true;
    }
  }

  ll p = 1;

  if (2 * min(k / 2, SZ(neg) / 2) + SZ(pos) >= k) {
    // can be positive

    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end(), greater<ll>());

    if (k % 2 == 1) {
      p *= pos.back();
      pos.pop_back();
      p %= MOD;
      k--;
    }

    vector<ll> fn;
    vector<ll> fp;

    for (int i = SZ(neg) - 1; i >= 1; i -= 2) {
      fn.pb((neg[i] * neg[i - 1]) % MOD);
    }
    for (int i = SZ(pos) - 1; i >= 1; i -= 2) {
      fp.pb((pos[i] * pos[i - 1]) % MOD);
    }

    fn.pb(-1);
    fp.pb(-1);

    reverse(fn.begin(), fn.end());
    reverse(fp.begin(), fp.end());

    while (k) {
      if (fp.back() < fn.back()) {
        swap(fn, fp);
      }
      p *= fp.back();
      fp.pop_back();
      p %= MOD;
      k -= 2;
    }

    cout << p << "\\n";
    exit(1);
  } else if (zero) {
    cout << 0 << "\\n";
  } else {

    vector<ll> absolute;
    for (auto v : neg) {
      absolute.pb(llabs(v));
    }
    for (auto v : pos) {
      absolute.pb(llabs(v));
    }
    sort(absolute.begin(), absolute.end());

    for (int i = 0; i < k; i++) {
      p *= absolute[i];
      p %= MOD;
    }
    p = MOD - p;

    cout << p << "\\n";
  }
}