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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
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;
  }
};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> vec(n);
  bool b = 0;
  rep(i, n) {
    cin >> vec[i];
    if (vec[i] >= 0)
      b = 1;
  }
  mint ans = 1;
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  if (b == 0 && k % 2 == 1) {
    rep(i, k) ans *= vec[i];
    cout << ans.x << endl;
    return 0;
  }
  if (n == k) {
    rep(i, n) ans *= vec[i];
    cout << ans.x << endl;
    return 0;
  }
  vector<ll> p, m;
  for (ll x : vec) {
    if (x >= 0)
      p.push_back(x);
    else
      m.push_back(x);
  }
  if (!m.empty())
    reverse(m.begin(), m.end());
  ll pnow = 0, mnow = 0;
  if (k % 2 == 1) {
    ans *= p[pnow];
    pnow++;
    k--;
  }
  while (k >= 2) {
    ll x = 0, y = 0;

    if (!p.empty()) {
      if (p.size() - 2 >= pnow)
        x = p[pnow] * p[pnow + 1];
    }
    if (!m.empty()) {
      if (m.size() - 2 >= mnow)
        y = m[mnow] * m[mnow + 1];
    }

    if (x >= y) {
      ans *= p[pnow];
      ans *= p[pnow + 1];
      pnow += 2;
      k -= 2;
    } else {
      ans *= m[mnow];
      ans *= m[mnow + 1];
      mnow += 2;
      k -= 2;
    }
  }

  cout << ans.x << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
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;
  }
};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> vec(n);
  bool b = 0;
  rep(i, n) {
    cin >> vec[i];
    if (vec[i] >= 0)
      b = 1;
  }
  mint ans = 1;
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  if (b == 0 && k % 2 == 1) {
    rep(i, k) ans *= vec[i];
    cout << ans.x << endl;
    return 0;
  }
  if (n == k) {
    rep(i, n) ans *= vec[i];
    cout << ans.x << endl;
    return 0;
  }
  vector<ll> p, m;
  for (ll x : vec) {
    if (x >= 0)
      p.push_back(x);
    else
      m.push_back(x);
  }
  if (!m.empty())
    reverse(m.begin(), m.end());
  ll pnow = 0, mnow = 0;
  if (k % 2 == 1) {
    ans *= p[pnow];
    pnow++;
    k--;
  }
  while (k > 2) {
    ll x = 0, y = 0;

    if (!p.empty()) {
      if (p.size() - 2 >= pnow)
        x = p[pnow] * p[pnow + 1];
    }
    if (!m.empty()) {
      if (m.size() - 2 >= mnow)
        y = m[mnow] * m[mnow + 1];
    }

    if (x >= y) {
      ans *= p[pnow];
      ans *= p[pnow + 1];
      pnow += 2;
      k -= 2;
    } else {
      ans *= m[mnow];
      ans *= m[mnow + 1];
      mnow += 2;
      k -= 2;
    }
  }
  if (k == 2) {
    ans *= p[pnow];
    ans *= m[mnow];
  }

  cout << ans.x << endl;
}