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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<mod>;

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

  int n, k;
  cin >> n >> k;
  vector<int> Positive, Negative;
  int ZeroCnt = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a > 0)
      Positive.emplace_back(a);
    else if (a < 0)
      Negative.emplace_back(-a);
    else
      ++ZeroCnt;
  }
  int n1 = Positive.size(), n2 = Negative.size();
  if (n1 + n2 < k) {
    cout << 0 << endl;
    return 0;
  }
  if (n1 + n2 == k) {
    modint ans = 1;
    for (int x : Positive)
      ans *= x;
    for (int x : Negative)
      ans *= x;
    if (n2 % 2)
      ans *= -1;
    cout << ans << endl;
    return 0;
  }
  if (n1 == 0) {
    modint ans = 1;
    if (k % 2) {
      sort(Negative.begin(), Negative.end());
      for (int i = 0; i < k; ++i) {
        ans *= Negative[i];
      }
      ans *= -1;
      if (ZeroCnt)
        ans = 0;
    } else {
      sort(Negative.rbegin(), Negative.rend());
      for (int i = 0; i < k; ++i) {
        ans *= Negative[i];
      }
    }
    cout << ans << endl;
    return 0;
  }
  sort(Positive.rbegin(), Positive.rend());
  sort(Negative.rbegin(), Negative.rend());

  int p1 = 0, p2 = 0;
  int c = 0;
  vector<int> P, N;
  int rest = k;
  while (rest-- > 0) {
    if (p1 == n1) {
      N.emplace_back(Negative[p2++]);
      ++c;
    } else if (p2 == n2) {
      P.emplace_back(Positive[p1++]);
    } else {
      if (Positive[p1] > Negative[p2]) {
        P.emplace_back(Positive[p1++]);
      } else {
        N.emplace_back(Negative[p2++]);
        ++c;
      }
    }
  }
  modint ans = 1;
  if (c % 2) {
    if (p1 == n1) {
      P.pop_back();
      N.emplace_back(Negative[p2]);
    } else if (p2 == n2) {
      N.pop_back();
      P.emplace_back(Positive[p1]);
    } else {
      if (!P.size()) {
        N.pop_back();
        P.emplace_back(Positive[p1]);
      } else {
        if ((ll)P.back() * Positive[p1] > (ll)N.back() * Negative[p2]) {
          N.pop_back();
          P.emplace_back(Positive[p1]);
        } else {
          P.pop_back();
          N.emplace_back(Negative[p2]);
        }
      }
    }
  }
  for (int x : P)
    ans *= x;
  for (int x : N)
    ans *= x;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<mod>;

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

  int n, k;
  cin >> n >> k;
  vector<int> Positive, Negative;
  int ZeroCnt = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a > 0)
      Positive.emplace_back(a);
    else if (a < 0)
      Negative.emplace_back(-a);
    else
      ++ZeroCnt;
  }
  int n1 = Positive.size(), n2 = Negative.size();
  if (n1 + n2 < k) {
    cout << 0 << endl;
    return 0;
  }
  if (n1 + n2 == k) {
    modint ans = 1;
    for (int x : Positive)
      ans *= x;
    for (int x : Negative)
      ans *= x;
    if (n2 % 2)
      ans *= -1;
    cout << ans << endl;
    return 0;
  }
  if (n1 == 0) {
    modint ans = 1;
    if (k % 2) {
      sort(Negative.begin(), Negative.end());
      for (int i = 0; i < k; ++i) {
        ans *= Negative[i];
      }
      ans *= -1;
    } else {
      sort(Negative.rbegin(), Negative.rend());
      for (int i = 0; i < k; ++i) {
        ans *= Negative[i];
      }
    }
    cout << ans << endl;
    return 0;
  }
  sort(Positive.rbegin(), Positive.rend());
  sort(Negative.rbegin(), Negative.rend());

  int p1 = 0, p2 = 0;
  int c = 0;
  vector<int> P, N;
  int rest = k;
  while (rest-- > 0) {
    if (p1 == n1) {
      N.emplace_back(Negative[p2++]);
      ++c;
    } else if (p2 == n2) {
      P.emplace_back(Positive[p1++]);
    } else {
      if (Positive[p1] > Negative[p2]) {
        P.emplace_back(Positive[p1++]);
      } else {
        N.emplace_back(Negative[p2++]);
        ++c;
      }
    }
  }
  modint ans = 1;
  if (c % 2) {
    if (p1 == n1) {
      P.pop_back();
      N.emplace_back(Negative[p2]);
    } else if (p2 == n2) {
      N.pop_back();
      P.emplace_back(Positive[p1]);
    } else {
      if ((ll)P.back() * Positive[p1] > (ll)N.back() * Negative[p2]) {
        N.pop_back();
        P.emplace_back(Positive[p1]);
      } else {
        P.pop_back();
        N.emplace_back(Negative[p2]);
      }
    }
  }
  for (int x : P)
    ans *= x;
  for (int x : N)
    ans *= x;
  cout << ans << endl;
}