Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i))
const ll MOD = 1000000007;
int main() {
  ll ans = 1;
  int N, K;
  cin >> N >> K;
  vector<ll> pos, neg;
  rep(i, N) {
    ll x;
    cin >> x;
    if (x >= 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());
  int p = pos.size(), n = neg.size();
  if (n == 0) {
    rep(i, K) ans = (ans * pos[i]) % MOD;
  } else if (p == 0) {
    if (K % 2 == 0)
      rep(i, K) ans = (ans * neg[i]) % MOD;
    else {
      rep(i, K) ans = (ans * neg[N - 1 - i]) % MOD;
      ans = (ans + MOD) % MOD;
    }
  } else if (K == N) {
    rep(i, p) ans = (ans * pos[i]) % MOD;
    rep(i, n) ans = (ans * neg[i]) % MOD;
    ans = (ans + MOD) % MOD;
  } else {
    int count = 0;
    int i = 0, j = 0;
    while (count < K) {
      if (i < p - 1 && j < n - 1) {
        int x = pos[i] * pos[i + 1];
        int y = neg[j] * neg[j + 1];
        if (x > y || count == K - 1) {
          ans = (ans * pos[i]) % MOD;
          count++;
          i++;
        } else {
          ans = (ans * (y % MOD) % MOD) % MOD;
          j += 2;
          count += 2;
        }
      } else if (j == n - 1) {
        ans = (ans * pos[i]) % MOD;
        count++;
        i++;
      } else {
        if (count < K - 1) {
          ans = (ans * (neg[j] * neg[j + 1] % MOD) % MOD) % MOD;
          j += 2;
          count += 2;
        } else {
          ans = (ans * pos[i]) % MOD;
          count++;
          i++;
        }
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i))
const ll MOD = 1000000007;
int main() {
  ll ans = 1;
  int N, K;
  cin >> N >> K;
  vector<ll> pos, neg;
  rep(i, N) {
    ll x;
    cin >> x;
    if (x >= 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());
  int p = pos.size(), n = neg.size();
  if (n == 0) {
    rep(i, K) ans = (ans * pos[i]) % MOD;
  } else if (p == 0) {
    if (K % 2 == 0)
      rep(i, K) ans = (ans * neg[i]) % MOD;
    else {
      rep(i, K) ans = (ans * neg[N - 1 - i]) % MOD;
      ans = (ans + MOD) % MOD;
    }
  } else if (K == N) {
    rep(i, p) ans = (ans * pos[i]) % MOD;
    rep(i, n) ans = (ans * neg[i]) % MOD;
    ans = (ans + MOD) % MOD;
  } else {
    int count = 0;
    int i = 0, j = 0;
    while (count < K) {
      if (i < p - 1 && j < n - 1) {
        ll x = pos[i] * pos[i + 1];
        ll y = neg[j] * neg[j + 1];
        if (x > y || count == K - 1) {
          ans = (ans * pos[i]) % MOD;
          count++;
          i++;
        } else {
          ans = (ans * (y % MOD) % MOD) % MOD;
          j += 2;
          count += 2;
        }
      } else if (j == n - 1) {
        ans = (ans * pos[i]) % MOD;
        count++;
        i++;
      } else {
        if (count < K - 1) {
          ans = (ans * (neg[j] * neg[j + 1] % MOD) % MOD) % MOD;
          j += 2;
          count += 2;
        } else {
          ans = (ans * pos[i]) % MOD;
          count++;
          i++;
        }
      }
    }
  }

  cout << ans << endl;
}