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

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
vector<ll> v[3];
ll ans = 1;
int K;
ll solve() {
  reverse(v[2].begin(), v[2].end());
  int cnt1 = 0, cnt2 = 0;
  while (cnt1 + 1 < (int)(v[0].size()) && cnt2 + 1 < (int)(v[2].size()) &&
         cnt1 + cnt2 + 2 <= K) {
    if (1ll * v[2][cnt2] * v[2][cnt2 + 1] > 1ll * v[0][cnt1] * v[0][cnt1 + 1])
      ans = (1ll * v[2][cnt2] * v[2][cnt2 + 1]) % MOD * ans % MOD, cnt2 += 2;
    else
      ans = (1ll * v[0][cnt1] * v[0][cnt1 + 1]) % MOD * ans % MOD, cnt1 += 2;
  }
  while (cnt2 + 1 < (int)(v[2].size()) && cnt2 + cnt1 + 2 <= K) {
    ans = ans * v[2][cnt2] % MOD * v[2][cnt2 + 1] % MOD;
    cnt2 += 2;
  }
  while (cnt1 + 1 < (int)(v[0].size()) && cnt2 + cnt1 + 2 <= K) {
    ans = ans * v[0][cnt1] % MOD * v[0][cnt1 + 1] % MOD;
    cnt1 += 2;
  }
  if (cnt2 + cnt1 < K)
    ans = ans * v[2][cnt2] % MOD;
  return (ans + MOD) % MOD;
}
bool can1() {
  int num = (int)(v[0].size()) / 2;
  for (int i = 0; i <= num; i++)
    if (i * 2 <= K && K - 2 * i <= (int)(v[2].size()))
      return 1;
  return 0;
}
bool can0() { return v[1].size() > 0; }
bool cmp(int x, int y) { return abs(x) < abs(y); }
int main() {
  int n;
  cin >> n >> K;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x < 0)
      v[0].push_back(x);
    if (x > 0)
      v[2].push_back(x);
    if (x == 0)
      v[1].push_back(x);
  }
  sort(v[0].begin(), v[0].end());
  sort(v[2].begin(), v[2].end());
  if (can1()) {
    cout << solve();
  } else if (can0()) {
    puts("0");
  } else {
    for (int i = 0; i < v[0].size(); i++)
      v[1].push_back(v[0][i]);
    for (int i = 0; i < v[2].size(); i++)
      v[1].push_back(v[2][i]);
    sort(v[1].begin(), v[1].end(), cmp);
    for (int i = 0; i < K; i++)
      ans = ans * v[1][i] % MOD;
    cout << (ans + MOD) % MOD;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
vector<ll> v[3];
ll ans = 1;
int K;
ll solve() {
  reverse(v[2].begin(), v[2].end());
  int cnt1 = 0, cnt2 = 0;
  while (cnt1 + 1 < (int)(v[0].size()) && cnt2 + 1 < (int)(v[2].size()) &&
         cnt1 + cnt2 + 2 <= K) {
    if (1ll * v[2][cnt2] * v[2][cnt2 + 1] > 1ll * v[0][cnt1] * v[0][cnt1 + 1])
      ans = (1ll * v[0][cnt2] * v[0][cnt2 + 1]) % MOD * ans % MOD, cnt2 += 2;
    else
      ans = (1ll * v[0][cnt1] * v[0][cnt1 + 1]) % MOD * ans % MOD, cnt1 += 2;
  }
  while (cnt2 + 1 < (int)(v[2].size()) && cnt2 + cnt1 + 2 <= K) {
    ans = ans * v[2][cnt2] % MOD * v[2][cnt2 + 1] % MOD;
    cnt2 += 2;
  }
  while (cnt1 + 1 < (int)(v[0].size()) && cnt2 + cnt1 + 2 <= K) {
    ans = ans * v[0][cnt1] % MOD * v[0][cnt1 + 1] % MOD;
    cnt1 += 2;
  }
  if (cnt2 + cnt1 < K)
    ans = ans * v[2][cnt2] % MOD;
  return (ans + MOD) % MOD;
}
bool can1() {
  int num = (int)(v[0].size()) / 2;
  for (int i = 0; i <= num; i++)
    if (i * 2 <= K && K - 2 * i <= (int)(v[2].size()))
      return 1;
  return 0;
}
bool can0() { return v[1].size() > 0; }
bool cmp(int x, int y) { return abs(x) < abs(y); }
int main() {
  int n;
  cin >> n >> K;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x < 0)
      v[0].push_back(x);
    if (x > 0)
      v[2].push_back(x);
    if (x == 0)
      v[1].push_back(x);
  }
  sort(v[0].begin(), v[0].end());
  sort(v[2].begin(), v[2].end());
  if (can1()) {
    cout << solve();
  } else if (can0()) {
    puts("0");
  } else {
    for (int i = 0; i < v[0].size(); i++)
      v[1].push_back(v[0][i]);
    for (int i = 0; i < v[2].size(); i++)
      v[1].push_back(v[2][i]);
    sort(v[1].begin(), v[1].end(), cmp);
    for (int i = 0; i < K; i++)
      ans = ans * v[1][i] % MOD;
    cout << (ans + MOD) % MOD;
  }
}