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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
using mint = Fp<1000000007>;

//--------------------------- Solve ---------------------------//

void solve() {
  int N, K;
  cin >> N >> K;
  vector<long long> A(N), plus, minus;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] >= 0)
      plus.push_back(A[i]);
    else
      minus.push_back(A[i]);
  }

  sort(plus.begin(), plus.end());
  sort(minus.rbegin(), minus.rend());
  sort(A.begin(), A.end(), [](int x, int y) { return abs(x) < abs(y); });

  bool ok;
  if (plus.size()) {
    if (N == K) {
      if (minus.size() % 2 == 0)
        ok = true;
      else
        ok = false;
    } else
      ok = true;
  } else {
    if (K % 2 == 0)
      ok = true;
    else
      ok = false;
  }

  mint ans = 1;
  if (ok) {
    if (K % 2 == 1) {
      ans *= plus.back();
      plus.pop_back();
      K--;
    }
    vector<long long> v;
    while (plus.size() >= 2) {
      long long x = plus.back();
      plus.pop_back();
      long long y = plus.back();
      plus.pop_back();
      v.push_back(x * y);
    }
    while (minus.size() >= 2) {
      long long x = minus.back();
      minus.pop_back();
      long long y = minus.back();
      minus.pop_back();
      v.push_back(x * y);
    }
    sort(v.rbegin(), v.rend());
    for (int i = 0; i < K / 2; i++)
      ans *= v[i];
  } else {
    for (int i = 0; i < K; i++)
      ans *= A[i];
  }

  cout << ans << '\\n';
}

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

  solve();

  return 0;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
using mint = Fp<1000000007>;

void solve() {
  int N, K;
  cin >> N >> K;
  priority_queue<int> plus, zero;
  priority_queue<int, vector<int>, greater<int>> minus;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a < 0)
      minus.push(a);
    if (a > 0)
      plus.push(a);
    if (a == 0)
      zero.push(a);
    A[i] = a;
  }

  sort(A.begin(), A.end(), greater<int>());

  mint ans = 1;

  if (plus.size() == 0) {

    if (K % 2 == 1) {
      if (zero.size() != 0) {
        cout << 0 << endl;
        return;
      }

      for (int i = 0; i < K; i++) {
        ans *= A[i];
      }
      cout << ans << endl;
      return;
    } else {
      for (int i = 0; i < K; i++) {
        ans *= A[N - i - 1];
      }
      cout << ans << endl;
      return;
    }
  }

  if (K > plus.size() + minus.size()) {
    cout << 0 << endl;
    return;
  }

  if (K == plus.size() + minus.size()) {
    while (!plus.empty()) {
      ans *= plus.top();
      plus.pop();
    }
    while (!minus.empty()) {
      ans *= minus.top();
      plus.pop();
    }

    cout << ans << endl;
    return;
  }

  int last_plus, last_minus;

  int minus_count = 0;

  for (int i = 0; i < K; i++) {
    if (plus.empty()) {
      ans *= minus.top();
      last_minus = minus.top();
      minus.pop();

      minus_count++;
    }
    if (minus.empty()) {
      ans *= plus.top();
      last_plus = plus.top();
      plus.pop();
    }

    int m = minus.top(), p = plus.top();
    if (p > -m) {
      ans *= p;
      last_plus = p;
      plus.pop();
    } else {
      ans *= m;
      last_minus = m;
      minus.pop();

      minus_count++;
    }
  }

  if (minus_count % 2 == 1) {
    if (plus.empty()) {
      ans /= last_plus;
      ans *= minus.top();
    }
    ans /= last_minus;
    ans *= plus.top();
  }

  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}