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

// I SELL YOU...!
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
const ll MOD = 1e9 + 7;
ll calc(ll x) {
  x %= MOD;
  return (x + MOD) % MOD;
}
signed main() {
  init_io();
  ll n, k, ans = 1;
  cin >> n >> k;
  vector<ll> a(n), po, ne;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      po.push_back(a[i]);
    if (a[i] < 0)
      ne.push_back(a[i]);
    if (k == n) {
      ans = calc(ans * a[i]);
    }
  }
  if (k == n) {
    cout << ans << endl;
    return 0;
  }
  ll posum = po.size(), nesum = ne.size();
  ll pdx = 0, ndx = 0;
  ll check = (nesum == n && k % 2 == 1);
  if (check) {
    vector<ll> v;
    for (auto &i : ne) {
      v.push_back(-i);
    }
    for (auto &i : po) {
      v.push_back(i);
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < k; i++) {
      ans = calc(ans * v[i]);
    }
    ans = calc(ans * -1);
    cout << ans << endl;
    return 0;
  }
  sort(po.begin(), po.end(), greater<ll>());
  sort(ne.begin(), ne.end());
  for (int i = 0; i < k; i += 2) {
    if (i == k - 1) {
      ans = calc(ans * po[pdx]);
    } else {
      ll v0, v1;
      v0 = v1 = -1e18;
      if (pdx + 1 < posum) {
        v0 = po[pdx] * po[pdx + 1];
      }
      if (ndx + 1 < nesum) {
        v1 = ne[ndx] * ne[ndx + 1];
      }
      if (v0 > v1) {
        v0 %= MOD;
        pdx += 2;
        ans = calc(ans * v0);
      } else {
        v1 %= MOD;
        ndx += 2;
        ans = calc(ans * v1);
      }
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

// I SELL YOU...!
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
const ll MOD = 1e9 + 7;
ll calc(ll x) {
  x %= MOD;
  return (x + MOD) % MOD;
}
signed main() {
  init_io();
  ll n, k, ans = 1;
  cin >> n >> k;
  vector<ll> a(n), po, ne, ze;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      po.push_back(a[i]);
    if (a[i] < 0)
      ne.push_back(a[i]);
    if (a[i] == 0)
      ze.push_back(a[i]);
  }
  ll posum = po.size(), nesum = ne.size();
  ll pdx = 0, ndx = 0;
  bool is_zero = (posum + nesum != n);
  if ((k > posum) && ((k - posum) % 2 != 0)) {
    if (is_zero) {
      cout << 0 << endl;
    } else {
      vector<ll> v;
      for (auto &i : ne) {
        v.push_back(-i);
      }
      for (auto &i : po) {
        v.push_back(i);
      }
      sort(v.begin(), v.end());
      for (int i = 0; i < k; i++) {
        ans = calc(ans * v[i]);
      }
      ans = calc(ans * -1);
      cout << ans << endl;
    }
    return 0;
  }
  sort(po.begin(), po.end(), greater<>());
  sort(ne.begin(), ne.end());
  for (int i = 0; i < k; i++) {
    if (i == k - 1) {
      ans = calc(ans * po[pdx]);
    } else {
      if (ndx == nesum - 1 || ndx == nesum) {
        ans = calc(ans * po[pdx]);
        pdx++;
      } else if (pdx == posum - 1 || pdx == posum) {
        ll v1 = ne[ndx] * ne[ndx + 1];
        v1 %= MOD;
        ndx += 2;
        ans = calc(ans * v1);
        i++;
      } else {
        ll v0 = po[pdx] * po[pdx + 1];
        ll v1 = ne[ndx] * ne[ndx + 1];
        if (v0 >= v1) {
          pdx += 2;
          v0 %= MOD;
          ans = calc(ans * v0);
          i++;
        } else {
          ndx += 2;
          v1 %= MOD;
          ans = calc(ans * v1);
          i++;
        }
      }
    }
  }
  cout << ans << endl;
}