Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;

  multiset<long long int> a, b;
  for (int i = 0; i < n; i++) {
    long long int num;
    cin >> num;

    if (num >= 0)
      a.insert(num);
    if (num < 0)
      b.insert(-num);
  }
  int negative = b.size();

  long long int ans = 1;
  if (k % 2 == 1 && negative == n) {
    for (int i = 0; i < k; i++) {
      ans *= *b.begin();
      ans %= MOD;
      b.erase(b.begin());
    }
    ans *= -1;
    ans = (ans % MOD + MOD) % MOD;
  } else {
    int count = 0;
    while (count < k) {
      if (count == k - 1 || b.size() <= 1) {
        ans *= *prev(a.end());
        ans %= MOD;
        a.erase(prev(a.end()));

        count += 1;
      } else if (a.size() <= 1) {
        ans *= *prev(b.end());
        ans %= MOD;
        b.erase(prev(b.end()));

        ans *= *prev(b.end());
        ans %= MOD;
        b.erase(prev(b.end()));

        count += 2;
      } else {
        auto a1 = prev(a.end());
        auto a2 = prev(a1);
        auto b1 = prev(b.end());
        auto b2 = prev(b1);

        if (*a1 * *a2 > *b1 * *b2) {
          ans *= *prev(a.end());
          ans %= MOD;
          a.erase(prev(a.end()));

          count += 1;
        } else {
          ans *= *prev(b.end());
          ans %= MOD;
          b.erase(prev(b.end()));

          ans *= *prev(b.end());
          ans %= MOD;
          b.erase(prev(b.end()));

          count += 2;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;

  multiset<long long int> a, b;
  for (int i = 0; i < n; i++) {
    long long int num;
    cin >> num;

    if (num >= 0)
      a.insert(num);
    if (num < 0)
      b.insert(-num);
  }
  int negative = b.size();

  long long int ans = 1;
  if (n == k) {
    for (auto aa : a) {
      ans *= aa;
      ans %= MOD;
    }
    for (auto bb : b) {
      ans *= bb;
      ans %= MOD;
    }
    if (b.size() % 2 == 1) {
      ans *= -1;
      ans = (ans % MOD + MOD) % MOD;
    }
  } else if (k % 2 == 1 && negative == n) {
    for (int i = 0; i < k; i++) {
      ans *= *b.begin();
      ans %= MOD;
      b.erase(b.begin());
    }
    ans *= -1;
    ans = (ans % MOD + MOD) % MOD;
  } else {
    int count = 0;
    while (count < k) {
      if (count == k - 1 || b.size() <= 1) {
        ans *= *prev(a.end());
        ans %= MOD;
        a.erase(prev(a.end()));

        count += 1;
      } else if (a.size() <= 1) {
        ans *= *prev(b.end());
        ans %= MOD;
        b.erase(prev(b.end()));

        ans *= *prev(b.end());
        ans %= MOD;
        b.erase(prev(b.end()));

        count += 2;
      } else {
        auto a1 = prev(a.end());
        auto a2 = prev(a1);
        auto b1 = prev(b.end());
        auto b2 = prev(b1);

        if (*a1 * *a2 > *b1 * *b2) {
          ans *= *prev(a.end());
          ans %= MOD;
          a.erase(prev(a.end()));

          count += 1;
        } else {
          ans *= *prev(b.end());
          ans %= MOD;
          b.erase(prev(b.end()));

          ans *= *prev(b.end());
          ans %= MOD;
          b.erase(prev(b.end()));

          count += 2;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}