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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, prod = 1, mod = 1e9 + 7;
  cin >> n >> k;
  lvector A(n), pos, neg;
  rep(i, n) {
    cin >> A[i];
    if (A[i] < 0)
      neg.emplace_back(A[i]);
    else
      pos.emplace_back(A[i]);
  }
  ll ps = pos.size(), ns = neg.size();
  bool ispos = false;
  if (ps > 0) {
    if (n == k)
      ispos = (ns % 2 == 0);
    else
      ispos = true;
  } else
    ispos = (k % 2 == 0);
  if (!ispos) {
    sort(ALL(A), [](ll x, ll y) { return abs(x) < abs(y); });
    rep(i, k) prod = (prod * A[i]) % mod;
    while (prod < 0)
      prod += mod;
  } else {
    sort(ALL(pos));
    sort(RALL(neg));
    ll q = 1, x;
    if (k % 2 == 1) {
      q = pos.back();
      pos.pop_back();
    }
    lvector V;
    while (pos.size() >= 2) {
      x = pos.back();
      pos.pop_back();
      x *= pos.back();
      pos.pop_back();
      V.emplace_back(x);
    }
    while (neg.size() >= 2) {
      x = neg.back();
      neg.pop_back();
      x *= neg.back();
      neg.pop_back();
      V.emplace_back(x);
    }
    sort(RALL(V));
    rep(i, k / 2) {
      V[i] %= mod;
      prod = (prod * V[i]) % mod;
    }
    prod = (prod * q) % mod;
  }
  print(prod);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, x, prod = 1, mod = 1e9 + 7;
  cin >> n >> k;
  lvector plus, minus, zero;
  rep(i, n) {
    cin >> x;
    if (x > 0)
      plus.push_back(x);
    else if (x < 0)
      minus.push_back(x);
    else
      zero.push_back(x);
  }
  ll ps = plus.size(), ms = minus.size(), zs = zero.size();
  if (ps) {
    if (k <= ps) {
      ll num = 0, ppos = 0, mpos = 0;
      sort(RALL(plus));
      sort(ALL(minus));
      while (num < k) {
        if (num + 2 <= k) {
          if (ms && mpos + 1 < ms) {
            if (plus[ppos] * plus[ppos + 1] >= minus[mpos] * minus[mpos + 1]) {
              prod = (prod * plus[ppos]) % mod;
              prod = (prod * plus[ppos + 1]) % mod;
              ppos += 2, num += 2;
            } else {
              prod = (prod * minus[ppos]) % mod;
              prod = (prod * minus[ppos + 1]) % mod;
              mpos += 2, num += 2;
            }
          } else {
            prod = (prod * plus[ppos]) % mod;
            ppos++, num++;
          }
        } else {
          prod = (prod * plus[ppos]) % mod;
          ppos++, num++;
        }
      }
    } else {
      if (ms >= k - ps) {
        if ((k - ps) % 2 == 0) {
          ll num = 0, ppos = 0, mpos = 0;
          sort(RALL(plus));
          sort(ALL(minus));
          while (num < k) {
            if (num + 2 <= k) {
              if (ms && mpos + 1 < ms && ppos + 1 < ps) {
                if (plus[ppos] * plus[ppos + 1] >=
                    minus[mpos] * minus[mpos + 1]) {
                  prod = (prod * plus[ppos]) % mod;
                  prod = (prod * plus[ppos + 1]) % mod;
                  ppos += 2, num += 2;
                } else {
                  prod = (prod * minus[ppos]) % mod;
                  prod = (prod * minus[ppos + 1]) % mod;
                  mpos += 2, num += 2;
                }
              } else {
                if (mpos + 1 >= ms) {
                  prod = (prod * plus[ppos]) % mod;
                  ppos++, num++;
                } else {
                  prod = (prod * minus[ppos]) % mod;
                  prod = (prod * minus[ppos + 1]) % mod;
                  mpos += 2, num += 2;
                }
              }
            } else {
              prod = (prod * plus[ppos]) % mod;
              ppos++, num++;
            }
          }
        } else {
          if (zs)
            prod = 0;
          else {
            lvector ab(plus);
            for (ll m : minus)
              ab.push_back(-m);
            sort(ALL(ab));
            rep(i, k) prod = (prod * ab[i]) % mod;
            prod = mod - prod;
          }
        }
      } else
        prod = 0;
    }
  } else {
    if (k % 2 == 0) {
      if (k / 2 <= ms / 2) {
        sort(ALL(minus));
        rep(i, k) prod = (prod * (-1 * minus[i])) % mod;
      } else
        prod = 0;
    } else {
      if (zs)
        prod = 0;
      else {
        sort(RALL(minus));
        rep(i, k) prod = (prod * (-1 * minus[i])) % mod;
        prod = mod - prod;
      }
    }
  }
  while (prod < 0)
    prod += mod;
  print(prod);
  return 0;
}