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

#include <bits/stdc++.h>

int main() {
  using namespace std;
  constexpr unsigned long MOD = 1000000007;
  unsigned long N, K;
  cin >> N >> K;
  if (N == K) {
    unsigned long ans{1};
    for (unsigned long i{0}; i < N; ++i) {
      long k;
      cin >> k;
      if (k > 0)
        (ans *= k) %= MOD;
      else
        (ans *= MOD - (-k % MOD)) %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  vector<unsigned long> pos, neg, zero;
  for (unsigned long i{0}; i < N; ++i) {
    long k;
    cin >> k;
    if (k > 0)
      pos.emplace_back(k);
    else if (k < 0)
      neg.emplace_back(-k);
    else
      zero.emplace_back(0);
  }
  if (pos.empty()) {
    copy(begin(zero), end(zero), back_inserter(neg));
    if (K & 1)
      sort(begin(neg), end(neg));
    else
      sort(begin(neg), end(neg), greater<>());
    unsigned long ans{1};
    for (unsigned long i{0}; i < K; ++i)
      (ans *= neg[i]) %= MOD;
    cout << (K & 1 ? (MOD - ans) % MOD : ans) << endl;
  } else {
    sort(begin(pos), end(pos));
    sort(begin(neg), end(neg));
    unsigned long ans{1};
    if (K & 1) {
      --K;
      ans *= pos.back();
      pos.pop_back();
    }
    while ((pos.size() > 1 || neg.size() > 1) && K) {
      if (pos.size() < 2 ||
          (neg.size() > 1 &&
           pos.back() * pos.end()[-2] < neg.back() * neg.end()[-2])) {
        (ans *= neg.back()) %= MOD;
        neg.pop_back();
        (ans *= neg.back()) %= MOD;
        neg.pop_back();
      } else {
        (ans *= pos.back()) %= MOD;
        pos.pop_back();
        (ans *= pos.back()) %= MOD;
        pos.pop_back();
      }
      K -= 2;
    }
    if (K)
      return 0 & puts("0");
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  using namespace std;
  constexpr unsigned long MOD = 1000000007;
  unsigned long N, K;
  cin >> N >> K;
  vector<unsigned long> pos, neg, zero;
  for (unsigned long i{0}; i < N; ++i) {
    long k;
    cin >> k;
    if (k > 0)
      pos.emplace_back(k);
    else if (k < 0)
      neg.emplace_back(-k);
    else
      zero.emplace_back(0);
  }
  if (pos.empty()) {
    copy(begin(zero), end(zero), back_inserter(neg));
    if (K & 1)
      sort(begin(neg), end(neg));
    else
      sort(begin(neg), end(neg), greater<>());
    unsigned long ans{1};
    for (unsigned long i{0}; i < K; ++i)
      (ans *= neg[i]) %= MOD;
    cout << (K & 1 ? (MOD - ans) % MOD : ans) << endl;
  } else {
    sort(begin(pos), end(pos));
    sort(begin(neg), end(neg));
    unsigned long ans{1};
    if (K & 1) {
      --K;
      ans *= pos.back();
      pos.pop_back();
    }
    while ((pos.size() > 1 || neg.size() > 1) && K) {
      if (pos.size() < 2 ||
          (neg.size() > 1 &&
           pos.back() * pos.end()[-2] < neg.back() * neg.end()[-2])) {
        (ans *= neg.back()) %= MOD;
        neg.pop_back();
        (ans *= neg.back()) %= MOD;
        neg.pop_back();
      } else {
        (ans *= pos.back()) %= MOD;
        pos.pop_back();
        (ans *= pos.back()) %= MOD;
        pos.pop_back();
      }
      K -= 2;
    }
    if (K)
      abort();
    else
      cout << ans << endl;
  }

  return 0;
}