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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t 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) { return mint(*this) /= a; }
};

istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> p, m;
  int cntp = 0, cntm = 0;
  rep(i, n) {
    int a;
    cin >> a;
    if (a >= 0) {
      p.push_back(a);
      cntp++;
    } else {
      m.push_back(-a);
      cntm++;
    }
  }
  sort(rall(p));
  sort(rall(m));
  if (p.size() and p[0] == 0 and k % 2) {
    cout << 0 << endl;
    return 0;
  }
  if (k == n) {
    mint ans = 1;
    rep(i, p.size()) ans *= p[i];
    rep(i, m.size()) ans *= -m[i];
    cout << ans << endl;
    return 0;
  }
  int j = 0;
  int jj = 0;
  mint ans = 1;
  rep(i, k) {
    if (j == p.size()) {
      ans *= m[jj++];
    } else if (jj == m.size()) {
      ans *= p[j++];
    } else {
      if (m[jj] > p[j]) {
        ans *= m[jj++];
      } else {
        ans *= p[j++];
      }
    }
  }
  if (ans.x == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (jj & 1) {
    if (j == 0) {
      if (p.size()) {
        ans = 1;
        rep(i, jj - 1) ans *= m[i];
        ans *= p[0];
      } else {
        reverse(all(m));
        ans = 1;
        rep(i, k) { ans *= m[i]; }
        ans = -ans;
      }
    } else {
      ans = 1;
      rep(i, j - 1) ans *= p[i];
      rep(i, jj - 1) ans *= m[i];
      if (j != p.size() and jj != m.size()) {
        ans *= max(p[j - 1] * p[j], m[jj - 1] * m[jj]);
      } else if (j != p.size()) {
        ans *= p[j - 1] * p[j];
      } else if (jj != m.size()) {
        ans *= m[jj - 1] * m[jj];
      }
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t 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) { return mint(*this) /= a; }
};

istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> p, m;
  int cntp = 0, cntm = 0;
  rep(i, n) {
    int a;
    cin >> a;
    if (a >= 0) {
      p.push_back(a);
      cntp++;
    } else {
      m.push_back(-a);
      cntm++;
    }
  }
  sort(rall(p));
  sort(rall(m));
  int j = 0;
  int jj = 0;
  mint ans = 1;
  rep(i, k) {
    if (j == p.size()) {
      ans *= m[jj++];
    } else if (jj == m.size()) {
      ans *= p[j++];
    } else {
      if (m[jj] > p[j]) {
        ans *= m[jj++];
      } else {
        ans *= p[j++];
      }
    }
  }
  if (jj & 1) {
    if (j == 0) {
      if (p.size()) {
        rep(i, jj - 1) ans *= m[i];
        ans *= p[0];
      } else {
        reverse(all(m));
        ans = 1;
        rep(i, k) { ans *= m[i]; }
        ans = -ans;
      }
    } else {
      ans = 1;
      rep(i, j - 1) ans *= p[i];
      rep(i, jj - 1) ans *= m[i];
      if (j != p.size() and jj != m.size()) {
        ans *= max(p[j - 1] * p[j], m[jj - 1] * m[j]);
      } else if (j != p.size()) {
        ans *= p[j - 1] * p[j];
      } else if (jj != m.size()) {
        ans *= m[jj - 1] * m[jj];
      }
    }
  }

  cout << ans << endl;
}