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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

const ll MOD = 1000000007;

struct mint {
  ll x;
  mint(ll 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 {
    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 / 2);
    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;
  }
};

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

int main() {
  ll N, K;
  cin >> N >> K;

  vec<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(all(A), greater<ll>());

  mint ans = 1;
  ll l = 0, r = N - 1;
  if (K % 2 == 1)
    ans *= A[0], l = 1;

  rep(_, K / 2) {
    if (A[l] * A[l + 1] > A[r] * A[r - 1]) {
      ans *= A[l] * A[l + 1];
      l += 2;
    } else {
      ans *= A[r] * A[r - 1];
      r -= 2;
    }
  }

  if (K % 2 == 1 && A[0] < 0) {
    ans = 1;
    rep(i, K) ans *= A[i];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

const ll MOD = 1000000007;

struct mint {
  ll x;
  mint(ll 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 {
    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 / 2);
    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;
  }
};

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

int main() {
  ll N, K;
  cin >> N >> K;

  vec<ll> A;
  vec<ll> P;
  vec<ll> M;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a >= 0)
      P.push_back(a);
    else
      M.push_back(a);

    A.push_back(a);
  }
  ll p = P.size();
  ll m = M.size();
  mint ans = 1;

  if (K - p - m == 0) {
    for (auto e : A)
      ans *= e;
    cout << ans << endl;
    return 0;
  }

  if (p == 0 && K % 2 == 0) {
    sort(all(M));
    rep(i, K) ans *= M[i];
    cout << ans << endl;
    return 0;
  }

  if (p == 0 && K % 2 == 1) {
    sort(all(M), greater<ll>());
    rep(i, K) ans *= M[i];
    cout << ans << endl;
    return 0;
  }

  if (K % 2 == 1) {
    ans *= A.back();
    A.pop_back();
    --K;
    --N;
  }

  sort(all(A), [](ll const &a, ll const &b) { return abs(a) > abs(b); });

  ll mnP = 1e9 + 7;
  ll mxM = -1e9 - 7;
  ll mcnt = 0;
  rep(i, K) {
    ans *= abs(A[i]);
    if (A[i] >= 0)
      mnP = min(mnP, A[i]);
    else {
      mxM = max(mxM, A[i]);
      ++mcnt;
    }
  }
  if (mcnt % 2 == 0) {
    cout << ans << endl;
    return 0;
  } else {
    ll cngP = -1;
    ll cngM = 1;
    for (int i = K; i < N; ++i) {
      if (A[i] >= 0)
        cngP = max(cngP, A[i]);
      if (A[i] < 0)
        cngM = min(cngM, A[i]);
    }
    if (cngP == -1) {
      ans /= mnP;
      ans *= cngM;
    } else if (cngM == -1) {
      ans /= mxM;
      ans *= cngP;
    } else if (cngM / mnP >= cngP / mxM) {
      ans /= mnP;
      ans *= cngM;
    } else {
      ans /= mxM;
      ans *= cngP;
    }
    cout << ans << endl;
  }
}