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

#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
using Int = long long;
const Int MOD = 1000000007;
int main() {
  int N, K;
  cin >> N >> K;
  vector<Int> A(N);
  for (auto &a : A)
    cin >> a;
  int pos = 0, zero = 0, neg = 0;
  for (auto a : A) {
    if (a > 0)
      pos++;
    if (a == 0)
      zero++;
    if (a < 0)
      neg++;
  }
  sort(begin(A), end(A));
  Int ans = 1;
  if (neg == 0) {
    for (int i = N - K; i < N; i++) {
      ans = (ans * A[i]) % MOD;
    }
  } else if (K == N) {
    for (int i = 0; i < N; i++) {
      if (A[i] < 0)
        A[i] = MOD - abs(A[i]);
      ans = (ans * A[i]) % MOD;
    }
  } else if (pos == 0 && K % 2 == 0) {
    for (int i = 0; i < K; i++) {
      ans = (ans * abs(A[i])) % MOD;
    }
  } else if (pos == 0 && K % 2 == 1) {
    for (int i = N - K; i < N; i++) {
      ans = (ans * abs(A[i])) % MOD;
    }
    ans = (ans * (MOD - 1)) % MOD;
  } else {
    vector<pair<Int, int>> B;
    for (auto a : A) {
      B.emplace_back(abs(a), a < 0);
    }
    sort(rbegin(B), rend(B));
    vector<Int> plus, minus;
    for (int i = 0; i < N; i++) {
      if (B[i].second == 0) {
        plus.push_back(B[i].first);
      } else {
        minus.push_back(B[i].first);
      }
    }
    int p = K, n = 0;
    while (p > plus.size()) {
      p--, n++;
    }
    if (n % 2 == 1)
      p--, n++;
    while (p >= 2 && n + 2 <= minus.size() &&
           plus[p - 1] * plus[p - 2] < minus[n] * minus[n + 1]) {
      p -= 2, n += 2;
    }
    for (int i = 0; i < p; i++)
      ans = (ans * plus[i]) % MOD;
    for (int i = 0; i < n; i++)
      ans = (ans * minus[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: 1

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
using Int = long long;
const Int MOD = 1000000007;
int main() {
  int N, K;
  cin >> N >> K;
  vector<Int> A(N);
  for (auto &a : A)
    cin >> a;
  int pos = 0, zero = 0, neg = 0;
  for (auto a : A) {
    if (a > 0)
      pos++;
    if (a == 0)
      zero++;
    if (a < 0)
      neg++;
  }
  sort(begin(A), end(A));
  Int ans = 1;
  if (neg == 0) {
    for (int i = N - K; i < N; i++) {
      ans = (ans * A[i]) % MOD;
    }
  } else if (K == N) {
    for (int i = 0; i < N; i++) {
      if (A[i] < 0)
        A[i] = MOD - A[i];
      ans = (ans * A[i]) % MOD;
    }
  } else if (pos == 0 && K % 2 == 0) {
    for (int i = 0; i < K; i++) {
      ans = (ans * abs(A[i])) % MOD;
    }
  } else if (pos == 0 && K % 2 == 1) {
    for (int i = N - K; i < N; i++) {
      ans = (ans * abs(A[i])) % MOD;
    }
    ans = (ans * (MOD - 1)) % MOD;
  } else {
    return 1;
    vector<pair<Int, int>> B;
    for (auto a : A) {
      B.emplace_back(abs(a), a < 0);
    }
    sort(rbegin(B), rend(B));
    vector<Int> plus, minus;
    for (int i = 0; i < N; i++) {
      if (B[i].second == 0) {
        plus.push_back(B[i].first);
      } else {
        minus.push_back(B[i].first);
      }
    }
    int p = K, n = 0;
    while (p > plus.size()) {
      p--, n++;
    }
    if (n % 2 == 1)
      p--, n++;
    while (p >= 2 && n + 2 <= minus.size() &&
           plus[p - 1] * plus[p - 2] < minus[n] * minus[n + 1]) {
      p -= 2, n += 2;
    }
    for (int i = 0; i < p; i++)
      ans = (ans * plus[i]) % MOD;
    for (int i = 0; i < n; i++)
      ans = (ans * minus[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}