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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll 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, const 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);
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n), B, C;
  rep(i, n) {
    cin >> A[i];
    if (A[i] < 0)
      C.push_back(A[i]);
    else
      B.push_back(A[i]);
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  sort(C.begin(), C.end());
  mint ans = 1;
  int cnt = 0;
  int bi = 0, ci = 0;
  if (k == n) {
    rep(i, k) ans *= mint(A[i]);
    cout << ans << endl;
  } else if ((int(C.size()) == n) && (k % 2 == 1)) {
    rep(i, k) ans *= mint(C[n - i - 1]);
    cout << ans << endl;
  } else {
    while (cnt != k) {
      if (bi >= (int(B.size()) - 1)) {
        if (((k - cnt) % 2) == 1) {
          ans *= mint(B[bi]);
          cnt++;
        }
        rep(i, k - cnt) { ans *= mint(C[ci + i]); }
        cnt = k;
      } else if (ci >= (int(C.size()) - 1)) {
        rep(i, k - cnt) { ans *= mint(B[bi + i]); }
        cnt = k;
      } else if ((cnt == (k - 1)) ||
                 ((B[bi] * B[bi + 1]) > (C[ci] * C[ci + 1]))) {
        ans *= mint(B[bi]);
        bi++;
        cnt++;
      } else {
        ans *= mint(C[ci]);
        ans *= mint(C[ci + 1]);
        ci += 2;
        cnt += 2;
      }
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll 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, const 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> A(n), B, C;
  rep(i, n) {
    cin >> A[i];
    if (A[i] < 0)
      C.push_back(A[i]);
    else
      B.push_back(A[i]);
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  sort(C.begin(), C.end());
  mint ans = 1;
  int cnt = 0;
  int bi = 0, ci = 0;
  if (k == n) {
    rep(i, k) ans *= mint(A[i]);
    cout << ans << endl;
  } else if ((C.size() == n) && (k % 2 == 1)) {
    rep(i, k) ans *= mint(C[n - i - 1]);
    cout << ans << endl;
  } else {
    while (cnt != k) {
      if (bi >= (B.size() - 1)) {
        if (((k - cnt) % 2) == 1) {
          ans *= mint(B[bi]);
          cnt++;
        }
        rep(i, k - cnt) { ans *= mint(C[ci + i]); }
        cnt = k;
      } else if (ci >= (C.size() - 1)) {
        rep(i, k - cnt) { ans *= mint(B[bi + i]); }
        cnt = k;
      } else if ((cnt == (k - 1)) ||
                 ((B[bi] * B[bi + 1]) > (C[ci] * C[ci + 1]))) {
        ans *= mint(B[bi]);
        bi++;
        cnt++;
      } else {
        ans *= mint(C[ci]);
        ans *= mint(C[ci + 1]);
        ci += 2;
        cnt += 2;
      }
    }
    cout << ans << endl;
  }
}