Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

lint Power(lint n, lint x, lint mod) {
  if (x == 0)
    return 1;
  lint res = Power(n, x / 2, mod);
  res = res * res % mod;
  if (x & 1)
    res = res * n % mod;
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(begin(a), end(a), [&](int i, int j) { return abs(i) < abs(j); });
  reverse(begin(a), end(a));
  lint ans = 1;
  const lint mod = 1e9 + 7;
  int neg = 0;
  for (int i = 0; i < k; i++) {
    ans *= abs(a[i]);
    ans %= mod;
    if (a[i] < 0) {
      neg++;
    }
    if (a[i] == 0) {
      cout << 0 << "\n";
      return 0;
    }
  }
  if (neg & 1) {
    int lastneg = -1;
    int lastpos = -1;
    int nextneg = -1;
    int nextpos = -1;
    for (int i = 0; i < k; i++) {
      if (a[i] < 0) {
        lastneg = i;
      }
      if (a[i] > 0) {
        lastpos = i;
        ;
      }
    }
    for (int i = k; i < n; i++) {
      if (nextneg == -1 && a[i] < 0) {
        nextneg = i;
      }
      if (nextpos == -1 && a[i] > 0) {
        nextpos = a[i];
      }
    }
    vector<pair<lint, lint>> ratios;
    if (lastneg != -1 && nextpos != -1) {
      ratios.emplace_back(abs(a[nextpos]), abs(a[lastneg]));
    }
    if (lastpos != -1 && nextneg != -1) {
      ratios.emplace_back(abs(a[nextneg]), abs(a[lastpos]));
    }
    sort(begin(ratios), end(ratios),
         [&](pair<lint, lint> p, pair<lint, lint> q) {
           return p.first * q.second < q.first * p.second;
         });
    if (!ratios.empty()) {
      ans *= Power(ratios.back().second, mod - 2, mod);
      ans %= mod;
      ans *= ratios.back().first;
      ans %= mod;
    } else {
      ans = 1;
      sort(begin(a), end(a));
      reverse(begin(a), end(a));
      for (int i = 0; i < k; i++) {
        ans *= a[i];
        ans %= mod;
      }
    }
  }
  if (ans < 0)
    ans += mod;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

lint Power(lint n, lint x, lint mod) {
  if (x == 0)
    return 1;
  lint res = Power(n, x / 2, mod);
  res = res * res % mod;
  if (x & 1)
    res = res * n % mod;
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(begin(a), end(a), [&](int i, int j) { return abs(i) < abs(j); });
  reverse(begin(a), end(a));
  lint ans = 1;
  const lint mod = 1e9 + 7;
  int neg = 0;
  for (int i = 0; i < k; i++) {
    ans *= abs(a[i]);
    ans %= mod;
    if (a[i] < 0) {
      neg++;
    }
    if (a[i] == 0) {
      cout << 0 << "\n";
      return 0;
    }
  }
  if (neg & 1) {
    int lastneg = -1;
    int lastpos = -1;
    int nextneg = -1;
    int nextpos = -1;
    for (int i = 0; i < k; i++) {
      if (a[i] < 0) {
        lastneg = i;
      }
      if (a[i] > 0) {
        lastpos = i;
        ;
      }
    }
    for (int i = k; i < n; i++) {
      if (nextneg == -1 && a[i] < 0) {
        nextneg = i;
      }
      if (nextpos == -1 && a[i] > 0) {
        nextpos = i;
      }
    }
    vector<pair<lint, lint>> ratios;
    if (lastneg != -1 && nextpos != -1) {
      ratios.emplace_back(abs(a[nextpos]), abs(a[lastneg]));
    }
    if (lastpos != -1 && nextneg != -1) {
      ratios.emplace_back(abs(a[nextneg]), abs(a[lastpos]));
    }
    sort(begin(ratios), end(ratios),
         [&](pair<lint, lint> p, pair<lint, lint> q) {
           return p.first * q.second < q.first * p.second;
         });
    if (!ratios.empty()) {
      ans *= Power(ratios.back().second, mod - 2, mod);
      ans %= mod;
      ans *= ratios.back().first;
      ans %= mod;
    } else {
      ans = 1;
      sort(begin(a), end(a));
      reverse(begin(a), end(a));
      for (int i = 0; i < k; i++) {
        ans *= a[i];
        ans %= mod;
      }
    }
  }
  if (ans < 0)
    ans += mod;
  cout << ans << "\n";
  return 0;
}