Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
// constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

  ll n, k;
  scanf("%lld %lld", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a.rbegin(), a.rend());
  vector<ll> vmn, vpl;
  ll z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      z++;
    else if (a[i] > 0)
      vpl.emplace_back(a[i]);
    else
      vmn.emplace_back(llabs(a[i]));
  }

  bool ok = false;
  if (k <= vpl.size())
    ok = true;
  else {
    for (ll i = 0; i <= vpl.size(); i++) {
      if ((k - i) % 2 == 1)
        continue;
      ll r = k - i;
      if (r <= vmn.size()) {
        ok = true;
        break;
      }
    }
  }
  if (!ok) {
    if (z > 0)
      puts("0");
    else {
      vector<ll> v;
      for (int i = 0; i < n; i++) {
        v.emplace_back(llabs(a[i]));
      }
      sort(v.begin(), v.end());
      mint res = 1;
      for (int i = 0; i < k; i++)
        res *= v[i];
      res *= -1;
      cout << res.x << "\n";
    }
    return 0;
  }

  sort(vmn.rbegin(), vmn.rend());
  sort(vpl.rbegin(), vpl.rend());

  mint res = 1;
  int ppos, mpos;
  ppos = mpos = 0;
  if (k & 1) {
    k--;
    res *= vpl[ppos++];
  }
  while (k > 0) {
    if (ppos + 1 < vpl.size() and mpos + 1 < vmn.size()) {
      ll val_pl = vpl[ppos] * vpl[ppos + 1];
      ll val_mn = vmn[mpos] * vmn[mpos + 1];
      if (val_mn > val_pl) {
        res *= val_mn % 1000000007;
        mpos += 2;
      } else {
        res *= val_pl % 1000000007;
        ppos += 2;
      }
    } else if (ppos + 1 < vpl.size()) {
      res *= vpl[ppos++];
      res *= vpl[ppos++];
    } else {
      res *= vpl[mpos++];
      res *= vpl[mpos++];
    }
    k -= 2;
  }
  cout << res.x << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
// constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

  ll n, k;
  scanf("%lld %lld", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a.rbegin(), a.rend());
  vector<ll> vmn, vpl;
  ll z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      z++;
    else if (a[i] > 0)
      vpl.emplace_back(a[i]);
    else
      vmn.emplace_back(llabs(a[i]));
  }

  bool ok = false;
  if (k <= vpl.size())
    ok = true;
  else {
    for (ll i = 0; i <= vpl.size(); i++) {
      if ((k - i) % 2 == 1)
        continue;
      ll r = k - i;
      if (r <= vmn.size()) {
        ok = true;
        break;
      }
    }
  }
  if (!ok) {
    if (z > 0)
      puts("0");
    else {
      vector<ll> v;
      for (int i = 0; i < n; i++) {
        v.emplace_back(llabs(a[i]));
      }
      sort(v.begin(), v.end());
      mint res = 1;
      for (int i = 0; i < k; i++)
        res *= v[i];
      res *= -1;
      cout << res.x << "\n";
    }
    return 0;
  }

  sort(vmn.rbegin(), vmn.rend());
  sort(vpl.rbegin(), vpl.rend());

  mint res = 1;
  int ppos, mpos;
  ppos = mpos = 0;
  if (k & 1) {
    k--;
    res *= vpl[ppos++];
  }
  while (k > 0) {
    if (ppos + 1 < vpl.size() and mpos + 1 < vmn.size()) {
      ll val_pl = vpl[ppos] * vpl[ppos + 1];
      ll val_mn = vmn[mpos] * vmn[mpos + 1];
      if (val_mn > val_pl) {
        res *= val_mn % 1000000007;
        mpos += 2;
      } else {
        res *= val_pl % 1000000007;
        ppos += 2;
      }
    } else if (ppos + 1 < vpl.size()) {
      res *= vpl[ppos++];
      res *= vpl[ppos++];
    } else {
      res *= vmn[mpos++];
      res *= vmn[mpos++];
    }
    k -= 2;
  }
  cout << res.x << "\n";
  return 0;
}