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

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int N, K;
vector<pair<ll, ll>> A;
vector<ll> p2, m2;

signed main() {
  cin >> N >> K;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a == 0)
      A.pb({0, 1});
    else
      A.pb({abs(a), a / abs(a)});
  }
  sort(all(A));
  reverse(all(A));

  if (K == N) {
    ll ans = 1;
    rep(i, N) {
      ans *= (A[i].first * A[i].second + MOD) % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  bool allM = true;
  rep(i, N) allM &= (A[i].second == -1);
  if (allM && K % 2 == 1) {
    ll ans = 1;
    reverse(all(A));
    rep(i, K) {
      ans *= MOD - A[i].first;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  if (allM && K % 2 == 0) {
    ll ans = 1;
    rep(i, K) {
      ans *= (A[i].first * A[i].second + MOD) % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  int cnt = 0;
  rep(i, K) {
    if (A[i].second == -1)
      cnt++;
  }
  if (cnt % 2 == 0) {
    ll ans = 1;
    rep(i, K) ans = ans * A[i].first % MOD;
    cout << ans << endl;
  } else {
    int p = -1, m = -1;
    rep(i, K) {
      if (A[i].second == 1)
        p = i;
      if (A[i].second == -1)
        m = i;
    }
    ll ans = 1;
    rep(i, K) {
      if (i == p || i == m)
        continue;
      ans = ans * A[i].first % MOD;
    }
    int p2 = -1, m2 = -1;
    for (int i = N - 1; i >= K; i--) {
      if (A[i].second == 1)
        p2 = i;
      if (A[i].second == -1)
        m2 = i;
    }

    if (p == -1) {
      ans *= A[p2].first;
      ans %= MOD;
      cout << ans << endl;
      return 0;
    }
    if (p == -1 || p2 == -1) {
      ans *= A[m].first;
      ans %= MOD;
      ans *= A[m2].first;
      ans %= MOD;
      cout << ans << endl;
      return 0;
    }
    if (m == -1 || m2 == -1) {
      ans *= A[p].first;
      ans %= MOD;
      ans *= A[p2].first;
      ans %= MOD;
      cout << ans << endl;
      return 0;
    }

    ll x1 = A[m].first * A[m2].first;
    ll x2 = A[p].first * A[p2].first;
    cout << max(x1, x2) % MOD * ans % MOD << endl;
  }
}

Error: 0

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int N, K;
vector<pair<ll, ll>> A;
vector<ll> p2, m2;

signed main() {
  cin >> N >> K;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a == 0)
      A.pb({0, 1});
    else
      A.pb({abs(a), a / abs(a)});
  }
  sort(all(A));
  reverse(all(A));

  if (K == N) {
    ll ans = 1;
    rep(i, N) {
      ans *= (A[i].first * A[i].second + MOD) % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  bool allM = true;
  rep(i, N) allM &= (A[i].second == -1);
  if (allM && K % 2 == 1) {
    ll ans = 1;
    reverse(all(A));
    rep(i, K) {
      ans *= MOD - A[i].first;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  if (allM && K % 2 == 0) {
    ll ans = 1;
    rep(i, K) {
      ans *= (A[i].first * A[i].second + MOD) % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  int cnt = 0;
  rep(i, K) {
    if (A[i].second == -1)
      cnt++;
  }
  if (cnt % 2 == 0) {
    ll ans = 1;
    rep(i, K) ans = ans * A[i].first % MOD;
    cout << ans << endl;
  } else {
    int p = -1, m = -1;
    rep(i, K) {
      if (A[i].second == 1)
        p = i;
      if (A[i].second == -1)
        m = i;
    }
    ll ans = 1;
    rep(i, K) {
      if (i == p || i == m)
        continue;
      ans = ans * A[i].first % MOD;
    }
    int p2 = -1, m2 = -1;
    for (int i = N - 1; i >= K; i--) {
      if (A[i].second == 1)
        p2 = i;
      if (A[i].second == -1)
        m2 = i;
    }

    if (p == -1 || p2 == -1) {
      ans *= A[m].first;
      ans %= MOD;
      ans *= A[m2].first;
      ans %= MOD;
      cout << ans << endl;
      return 0;
    }
    if (m == -1 || m2 == -1) {
      ans *= A[p].first;
      ans %= MOD;
      ans *= A[p2].first;
      ans %= MOD;
      cout << ans << endl;
      return 0;
    }

    assert(false);
    return -1;

    ll x1 = A[m].first * A[m2].first;
    ll x2 = A[p].first * A[p2].first;
    cout << max(x1, x2) % MOD * ans % MOD << endl;
  }
}