Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), neg, pos;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] <= 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }
  sort(ALL(neg));
  sort(ALL(pos));
  constexpr ll mod = 1000000000 + 7;
  // n == k
  if (n == k) {
    ll ans = 1;
    rep(i, 0, k) ans = (ans * a[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }
  // all negative or zero
  if (pos.empty()) {
    ll ans = 1;
    if (k % 2 == 1)
      reverse(neg.begin(), neg.end());
    rep(i, 0, k) ans = (ans * neg[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }
  cerr << "exp" << endl;
  // こんときは必ず正の値にすることができる気がする？
  ll pi = pos.size() - 1, ni = 0;
  ll ans = 1;
  if (k % 2 == 1) {
    ans *= pos[pos.size() - 1];
    ans %= mod;
    k--;
    pi -= 1;
  }
  rep(i, 0, k / 2) {
    ll tp = -1, tn = -1;
    if (pi > 0)
      tp = pos[pi] * pos[pi - 1];
    if (ni < neg.size() - 1)
      tn = neg[ni] * neg[ni + 1];
    if (tp > tn) {
      ans *= tp % mod;
      pi -= 2;
    } else {
      ans *= tn % mod;
      ni += 2;
    }
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), neg, pos;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] <= 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }
  sort(ALL(neg));
  sort(ALL(pos));
  constexpr ll mod = 1000000000 + 7;
  // n == k
  if (n == k) {
    ll ans = 1;
    rep(i, 0, k) ans = (ans * a[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }
  // all negative or zero
  if (pos.empty()) {
    ll ans = 1;
    if (k % 2 == 1)
      reverse(neg.begin(), neg.end());
    rep(i, 0, k) ans = (ans * neg[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }
  if (neg.empty()) {
    ll ans = 1;
    reverse(pos.begin(), pos.end());
    rep(i, 0, k) ans = (ans * pos[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }
  cerr << "exp" << endl;
  // こんときは必ず正の値にすることができる気がする？
  ll pi = pos.size() - 1, ni = 0;
  ll ans = 1;
  if (k % 2 == 1) {
    ans *= pos[pos.size() - 1];
    ans %= mod;
    k--;
    pi -= 1;
  }
  rep(i, 0, k / 2) {
    ll tp = -1, tn = -1;
    if (pi > 0)
      tp = pos[pi] * pos[pi - 1];
    if (ni < neg.size() - 1)
      tn = neg[ni] * neg[ni + 1];
    if (tp > tn) {
      ans *= tp % mod;
      pi -= 2;
    } else {
      ans *= tn % mod;
      ni += 2;
    }
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}