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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
const double PI = acos(-1);
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 { 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;
  }
  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() {
  int n, k;
  cin >> n >> k;
  vl 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]);
    }
  }
  mint ans = 1;
  bool mi = false;
  if (b.size() > 0) {
    if (n == k)
      mi = (c.size() % 2 == 0);
    else
      mi = true;
  } else {
    mi = (k % 2 == 0);
  }
  if (mi) {
    sort(been(a));
    sort(been(b));
    sort(been(c), greater<ll>());
    vl memo;
    if (k % 2) {
      ans *= b.back();
      b.pop_back();
    }
    while (b.size() > 1) {
      ll sum = b.back();
      b.pop_back();
      sum *= b.back();
      b.pop_back();
      memo.push_back(sum);
    }
    while (c.size() > 1) {
      ll sum = c.back();
      c.pop_back();
      sum *= c.back();
      c.pop_back();
      memo.push_back(sum);
    }
    sort(been(memo), greater<ll>());
    rep(i, k / 2) { ans *= memo[i]; }
    cout << ans << endl;
  } else {
    sort(been(a), [](ll x, ll y) { return abs(x) < abs(y); });
    rep(i, k) { ans *= a[i]; }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, n) for (int i = a; i < n; i++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int, int> P;
#define foreach(ix, i) for (auto &(ix) : (i))
typedef long double ld;
const int mod = 1000000007;
const double PI = acos(-1);
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 { 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;
  }
  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() {
  int n, k;
  cin >> n >> k;
  vl a(n), b, c;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      b.push_back(a[i]);
    else {
      c.push_back(a[i]);
    }
  }
  mint ans = 1;
  bool mi = false;
  if (b.size() > 0) {
    if (n == k)
      mi = (c.size() % 2 == 0);
    else
      mi = true;
  } else {
    mi = (k % 2 == 0);
  }
  if (mi) {
    sort(been(a));
    sort(been(b));
    sort(been(c), greater<ll>());
    vl memo;
    if (k % 2) {
      ans = b[n - 1];
      b.pop_back();
    }
    while (b.size() > 1) {
      ll sum = b.back();
      b.pop_back();
      sum *= b.back();
      b.pop_back();
      memo.push_back(sum);
    }
    while (c.size() > 1) {
      ll sum = c.back();
      c.pop_back();
      sum *= c.back();
      c.pop_back();
      memo.push_back(sum);
    }
    sort(been(memo), greater<ll>());
    rep(i, k / 2) { ans *= memo[i]; }
    cout << ans << endl;
  } else {
    sort(been(a), [](ll x, ll y) { return abs(x) < abs(y); });
    rep(i, k) { ans *= a[i]; }
    cout << ans << endl;
  }
}