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

#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for (i64 i = 0; i < N; i++)
#define Rep(i, k, N) for (i64 i = k; i < N; i++)
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define Cout(s) cout << s << endl
string Yes(bool b) {
  if (b) {
    return "Yes";
  } else {
    return "No";
  }
}
string YES(bool b) {
  if (b) {
    return "YES";
  } else {
    return "NO";
  }
}

#include <cstdint>
#include <iostream>

struct mint {
  using i64 = int_fast64_t;
  const i64 MOD = 1000000007;
  i64 x;

  mint(const i64 y = (i64)0) noexcept : x(y) {
    if (x >= MOD)
      x %= MOD;
    if (x < 0)
      x += MOD;
  }
  mint operator+() const noexcept { return x; }
  mint operator-() const noexcept { return x ? MOD - x : 0; }
  mint operator+(const mint y) const noexcept { return (mint) * this += y; }
  mint operator-(const mint y) const noexcept { return (mint) * this -= y; }
  mint operator*(const mint y) const noexcept { return (mint) * this *= y; }
  mint operator/(const mint y) const noexcept { return (mint) * this /= y; }

  mint operator=(const mint y) noexcept {
    x = y.x;
    if (x >= MOD)
      x -= MOD;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint operator=(const i64 y) noexcept {
    x = y;
    if (x >= MOD)
      x -= MOD;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator+=(const mint y) noexcept {
    x += y.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint y) noexcept {
    x -= y.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint y) noexcept {
    x *= y.x;
    if (x >= MOD)
      x %= MOD;
    while (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator/=(mint y) noexcept {
    i64 exp = MOD - 2;
    while (exp) {
      if (exp & 1)
        *this *= y;
      y *= y;
      exp /= 2;
    }
    return *this;
  }
  friend std::ostream &operator<<(std::ostream &os, const mint &m) noexcept {
    os << m.x;
    return os;
  }
  friend std::istream &operator>>(std::istream &in, mint &m) noexcept {
    const int mod = 1000000007;
    in >> m.x;
    if (m.x < 0)
      m.x += mod;
    if (m.x >= mod)
      m.x %= mod;
    return in;
  }
  friend mint mod_pow(mint n, i64 k) noexcept {
    mint res = 1;
    while (k) {
      if (k & 1)
        res *= n;
      n *= n;
      k >>= 1;
    }
    return res;
  }
};

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

  vector<i64> pos, neg;
  rep(i, N) {
    i64 a;
    cin >> a;
    if (a < 0)
      neg.emplace_back(a);
    else
      pos.emplace_back(a);
  }

  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());

  mint ans = 1;
  if (N == K) {
    for (auto &a : pos)
      ans *= a;
    for (auto &a : neg)
      ans *= a;
  } else if (pos.size() == 0 && K % 2) {
    rep(i, K) ans *= neg[i];
  } else {
    if (K % 2 && pos.size() != 0) {
      ans *= pos.back();
      pos.pop_back();
      K--;
    }
    for (int i = 0; i < K; i += 2) {
      i64 P = 0, N = 0;
      size_t pos_len = pos.size();
      size_t neg_len = neg.size();
      if (pos_len > 1)
        P = pos[pos_len - 1] * pos[pos_len - 2];
      if (neg_len > 1)
        N = neg[neg_len - 1] * neg[neg_len - 2];
      if (P <= N) {
        ans *= N;
        rep(i, 2) if (neg.size() > 0) neg.pop_back();
      } else {
        ans *= P;
        rep(i, 2) if (pos.size() > 0) pos.pop_back();
      }
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for (i64 i = 0; i < N; i++)
#define Rep(i, k, N) for (i64 i = k; i < N; i++)
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define Cout(s) cout << s << endl
string Yes(bool b) {
  if (b) {
    return "Yes";
  } else {
    return "No";
  }
}
string YES(bool b) {
  if (b) {
    return "YES";
  } else {
    return "NO";
  }
}

#include <cstdint>
#include <iostream>

struct mint {
  using i64 = int_fast64_t;
  const i64 MOD = 1000000007;
  i64 x;

  mint(const i64 y = (i64)0) noexcept : x(y) {
    if (x >= MOD)
      x %= MOD;
    if (x < 0)
      x += MOD;
  }
  mint operator+() const noexcept { return x; }
  mint operator-() const noexcept { return x ? MOD - x : 0; }
  mint operator+(const mint y) const noexcept { return (mint) * this += y; }
  mint operator-(const mint y) const noexcept { return (mint) * this -= y; }
  mint operator*(const mint y) const noexcept { return (mint) * this *= y; }
  mint operator/(const mint y) const noexcept { return (mint) * this /= y; }

  mint operator=(const mint y) noexcept {
    x = y.x;
    if (x >= MOD)
      x -= MOD;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint operator=(const i64 y) noexcept {
    x = y;
    if (x >= MOD)
      x -= MOD;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator+=(const mint y) noexcept {
    x += y.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint y) noexcept {
    x -= y.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint y) noexcept {
    x *= y.x;
    if (x >= MOD)
      x %= MOD;
    while (x < 0)
      x += MOD;
    return *this;
  }
  mint &operator/=(mint y) noexcept {
    i64 exp = MOD - 2;
    while (exp) {
      if (exp & 1)
        *this *= y;
      y *= y;
      exp /= 2;
    }
    return *this;
  }
  friend std::ostream &operator<<(std::ostream &os, const mint &m) noexcept {
    os << m.x;
    return os;
  }
  friend std::istream &operator>>(std::istream &in, mint &m) noexcept {
    const int mod = 1000000007;
    in >> m.x;
    if (m.x < 0)
      m.x += mod;
    if (m.x >= mod)
      m.x %= mod;
    return in;
  }
  friend mint mod_pow(mint n, i64 k) noexcept {
    mint res = 1;
    while (k) {
      if (k & 1)
        res *= n;
      n *= n;
      k >>= 1;
    }
    return res;
  }
};

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

  vector<i64> pos, neg;
  rep(i, N) {
    i64 a;
    cin >> a;
    if (a < 0)
      neg.emplace_back(a);
    else
      pos.emplace_back(a);
  }

  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());

  mint ans = 1;
  if (N == K) {
    for (auto &a : pos)
      ans *= a;
    for (auto &a : neg)
      ans *= a;
  } else if (pos.size() == 0 && K % 2) {
    rep(i, K) ans *= neg[i];
  } else {
    if (K % 2 && pos.size() != 0) {
      ans *= pos.back();
      pos.pop_back();
      K--;
    }
    for (int i = 0; i < K; i += 2) {
      i64 P = 0, N = 0;
      size_t pos_len = pos.size();
      size_t neg_len = neg.size();
      if (pos_len > 1)
        P = pos[pos_len - 1] * pos[pos_len - 2];
      if (neg_len > 1)
        N = neg[neg_len - 1] * neg[neg_len - 2];
      if (P <= N) {
        ans *= N;
        neg.pop_back();
        neg.pop_back();
      } else {
        ans *= P;
        pos.pop_back();
        pos.pop_back();
      }
    }
  }

  cout << ans << endl;
}