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

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const ll MOD = TEN(9) + 7;
int n, k;
vl non_neg, neg, a;

int main() {
  fastio();

  cin >> n >> k;

  forn(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      non_neg.push_back(x);
    } else if (x < 0) {
      neg.push_back(-x);
    }

    a.push_back(x);
  }

  if (k == n) {
    ll ans = 1;
    for (auto x : a) {
      ans = ans * abs(x) % MOD;
    }
    if (neg.size() % 2 == 1) {
      ans = (MOD - ans) % MOD;
    }
    cout << ans << endl;
    return 0;
  }

  sort(all(non_neg));
  sort(all(neg));

  ll ans = 1;
  if (neg.size() == n and k % 2) {
    for (int i = 0; i < k; i++) {
      ans = ans * neg[i] % MOD;
    }
    ans = (MOD - ans) % MOD;

    cout << ans << endl;
  } else {
    reverse(all(non_neg));
    reverse(all(neg));
    int c = 0;
    int i = 0, j = 0;

    while (c < k) {

      if (c + 1 == k or j >= int(neg.size()) - 1 or
          (i < int(non_neg.size()) - 1 and
           non_neg[i] * non_neg[i + 1] >= neg[j] * neg[j + 1])) {
        ans = ans * non_neg[i] % MOD;
        i++;
        c++;
      } else {
        ans = ans * neg[j] % MOD * neg[j + 1] % MOD;
        j += 2;
        c += 2;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const ll MOD = TEN(9) + 7;
int n, k;
vl non_neg, neg, a;

int main() {
  fastio();

  cin >> n >> k;

  forn(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      non_neg.push_back(x);
    } else if (x < 0) {
      neg.push_back(-x);
    }

    a.push_back(x);
  }

  if (k == n) {
    ll ans = 1;
    for (auto x : a) {
      ans = ans * abs(x) % MOD;
    }
    if (neg.size() % 2 == 1) {
      ans = (MOD - ans) % MOD;
    }
    cout << ans << endl;
    return 0;
  }

  sort(all(non_neg));
  sort(all(neg));

  ll ans = 1;
  if (neg.size() == n and k % 2) {
    for (int i = 0; i < k; i++) {
      ans = ans * neg[i] % MOD;
    }
    ans = (MOD - ans) % MOD;

    cout << ans << endl;
  } else {
    reverse(all(non_neg));
    reverse(all(neg));
    int c = 0;
    int i = 0, j = 0;

    while (c < k) {
      if (c + 1 == k or j >= int(neg.size()) - 1 or
          (i < int(non_neg.size()) - 1 and
           non_neg[i] * non_neg[i + 1] >= neg[i] * neg[i + 1])) {
        ans = ans * non_neg[i] % MOD;
        i++;
        c++;
      } else {
        ans = ans * neg[j] % MOD * neg[j + 1] % MOD;
        j += 2;
        c += 2;
      }
    }

    cout << ans << endl;
  }

  return 0;
}