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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 1000000007;
int main(int argc, char const *argv[]) {
  ll n, k;
  std::cin >> n >> k;
  vll a(n);
  ll pl = 0, mi = 0;
  ll x = 1;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    x = (x * ((a[i] + P) % P)) % P;
    if (a[i] > 0)
      pl++;
    else
      mi++;
  }
  ll ans = 1;
  sort(all(a), [](ll l, ll r) { return abs(l) < abs(r); });
  reverse(all(a));
  ll max_mi = min(k, mi);
  ll min_mi = max(0LL, k - pl);
  ll cnt = 0;

  if (min_mi == max_mi && min_mi % 2) {
    // 偶数個のマイナスができない場合
    // マイナスは全て取らないと
    reverse(all(a));
    for (int i = 0; i < n && cnt < k; i++) {
      if (a[i] <= 0)
        cnt++, ans = (ans * ((a[i] + P) % P)) % P;
      else
        cnt++, ans = (ans * ((a[i] + P) % P)) % P;
    }
    std::cout << ans << '\\n';
    return 0;
  }

  vll minus, plus;
  for (int i = 0; i < n; i++) {
    if (a[i] <= 0)
      minus.push_back(a[i]);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] > 0) {
      if (k % 2)
        ans = a[i], k--;
      else
        plus.push_back(a[i]);
    }
  }
  // for(auto t:minus) std::cout << t << '\\n';
  // for(auto t:plus) std::cout << t << '\\n';

  ll p_idx = 0, m_idx = 0;
  for (int i = 0; i < k; i += 2) {
    ll L =
        (p_idx * 2 + 2 > plus.size() ? 0
                                     : plus[p_idx * 2] * plus[p_idx * 2 + 1]);
    ll R = (m_idx * 2 + 2 > minus.size()
                ? 0
                : minus[m_idx * 2] * minus[m_idx * 2 + 1]);
    if (L > R)
      p_idx++, ans = (ans * (L % P)) % P;
    else
      m_idx++, ans = (ans * (R % P)) % P;
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 1000000007;
int main(int argc, char const *argv[]) {
  ll n, k;
  std::cin >> n >> k;
  vll a(n);
  ll pl = 0, mi = 0;
  ll x = 1;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    x = (x * ((a[i] + P) % P)) % P;
    if (a[i] > 0)
      pl++;
    else
      mi++;
  }
  ll ans = 1;
  sort(all(a), [](ll l, ll r) { return abs(l) < abs(r); });
  reverse(all(a));
  ll max_mi = min(k, mi);
  ll min_mi = max(0LL, k - pl);
  ll mcnt = 0;
  ll cnt = 0;

  if (min_mi == max_mi && min_mi % 2) {
    // 偶数個のマイナスができない場合
    // マイナスは全て取らないと
    reverse(all(a));
    for (int i = 0; i < n && cnt < k; i++) {
      if (a[i] <= 0)
        cnt++, ans = (ans * ((a[i] + P) % P)) % P;
      else
        cnt++, ans = (ans * ((a[i] + P) % P)) % P;
    }
    std::cout << ans << '\\n';
    return 0;
  }
  vll minus, plus;

  for (int i = 0; i < n; i++)
    if (a[i] <= 0)
      minus.push_back(a[i]);
  for (int i = 0; i < n; i++) {
    if (a[i] > 0) {
      if (k % 2)
        ans = a[i], k--;
      else
        plus.push_back(a[i]);
    }
  }

  ll p_idx = 0, m_idx = 0;
  for (int i = 0; i < k; i += 2) {
    ll L =
        (p_idx * 2 + 2 > plus.size() ? 0
                                     : plus[p_idx * 2] * plus[p_idx * 2 + 1]);
    ll R = (m_idx * 2 + 2 > minus.size()
                ? 0
                : minus[p_idx * 2] * minus[p_idx * 2 + 1]);
    if (L > R)
      p_idx++, ans = (ans * (L % P)) % P;
    else
      m_idx++, ans = (ans * (R % P)) % P;
  }
  std::cout << ans << '\\n';
  return 0;
}