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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  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 { 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(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 { 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; }

void solve(long long N, long long K, std::vector<long long> A) {
  mint ans = 1;
  if (K == N) {
    for (int i = 0; i < N; i++) {
      ans *= mint(A[i]);
    }
    cout << ans << endl;
    return;
  }
  // 0以下しかない時小さい順に選ぶ
  int notPlus = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= 0) {
      notPlus++;
    }
  }
  if (N == notPlus && K % 2 == 1) {
    sort(A.rbegin(), A.rend());
    for (int i = 0; i < K; i++) {
      ans *= mint(A[i]);
    }
    cout << ans << endl;
    return;
  }
  sort(A.begin(), A.end());
  int lindex = 0;
  int rindex = N - 1;
  ll minusNum = 0;
  for (int i = 0; i < K; i++) {
    if (abs(A[lindex]) >= abs(A[rindex])) {
      ans *= mint(A[lindex]);
      if (A[lindex] < 0) {
        minusNum++;
      }
      lindex++;
    } else {
      ans *= mint(A[rindex]);
      rindex--;
    }
  }
  // 結果がマイナスになった時,rindexを減らすか、lindexを減らす。
  //  cerr <<"minus=" <<minusNum <<"lindex="<<lindex <<"rindex=" << rindex<<
  //  endl;
  if (minusNum % 2 == 1) {
    // cerr <<"minus" << endl;
    if (rindex < N - 1 &&
        (A[lindex] * A[lindex - 1] > A[rindex] * A[rindex + 1])) {
      ans /= mint(A[rindex + 1]);
      ans *= mint(A[lindex]);
    } else {
      ans /= mint(A[minusNum - 1]);
      ans *= mint(A[rindex]);
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  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 { 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(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 { 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; }

void solve(long long N, long long K, std::vector<long long> A) {
  mint ans = 1;
  if (K == N) {
    for (int i = 0; i < N; i++) {
      ans *= mint(A[i]);
    }
    cout << ans << endl;
    return;
  }
  // 0以下しかない時小さい順に選ぶ
  int notPlus = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= 0) {
      notPlus++;
    }
  }
  if (N == notPlus) {
    sort(A.rbegin(), A.rend());
    for (int i = 0; i < K; i++) {
      ans *= mint(A[i]);
    }
    cout << ans << endl;
    return;
  }
  sort(A.begin(), A.end());
  int lindex = 0;
  int rindex = N - 1;
  ll minusNum = 0;
  for (int i = 0; i < K; i++) {
    if (abs(A[lindex]) > abs(A[rindex])) {
      ans *= mint(A[lindex]);
      if (A[lindex] < 0) {
        minusNum++;
      }
      lindex++;
    } else {
      ans *= mint(A[rindex]);
      rindex++;
    }
  }
  // 結果がマイナスになった時
  if (minusNum % 2 == 1) {
    ans /= mint(A[minusNum - 1]);
    ans *= A[rindex];
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  return 0;
}