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

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll test[12345678];
signed main() {
  ll n, k, cnt = 1;
  cin >> n >> k;
  vector<ll> sei, hu;
  rep(i, n) {
    cin >> test[i];
    cnt *= abs(test[i]);
    cnt %= mod;
    if (test[i] >= 0) {
      sei.push_back(test[i]);
    } else {
      hu.push_back(abs(test[i]));
    }
  }
  if (n == k) {
    if (hu.size() % 2 == 1) {
      cout << mod - cnt << endl;
    } else {
      cout << cnt << endl;
    }
    return 0;
  }
  ll a = 0, b = 0, ans = 1;
  if (k % 2 == 1 && sei.size() == 0) { // 負
    sort(all(hu));
    rep(i, k) {
      ans *= hu[i];
      ans %= mod;
    }
    cout << mod - ans << endl;
    return 0;
  }
  sort(all(sei));
  reverse(all(sei));
  sort(all(hu));
  reverse(all(hu));
  ll z = 0, q1 = sei.size(), w2 = hu.size();
  q1--;
  w2--;
  while ((a + b) != k) {
    if ((a + b) == k - 1) {
      ans *= sei[a];
      a++;
    } else if (a >= q1) {
      ans *= ((hu[b] * hu[b + 1]) % mod);
      b += 2;
    } else if (b >= w2) {
      ans *= (sei[a] % mod);
      a++;
    } else {
      if ((sei[a] * sei[a + 1]) > (hu[b] * hu[b + 1])) {
        ans *= sei[a];
        a++;
      } else {
        ans *= (hu[b] * hu[b + 1] % mod);
        b += 2;
      }
    }
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll test[12345678];
signed main() {
  ll n, k, cnt = 1;
  cin >> n >> k;
  vector<ll> sei, hu;
  ll zero = 0;
  rep(i, n) {
    cin >> test[i];
    cnt *= abs(test[i]);
    cnt %= mod;
    if (test[i] > 0) {
      sei.push_back(test[i]);
    } else if (test[i] == 0) {
      zero++;
    } else {
      hu.push_back(abs(test[i]));
    }
  }
  if (n == k) {
    if (hu.size() % 2 == 1) {
      cout << mod - cnt << endl;
    } else {
      cout << cnt << endl;
    }
    return 0;
  }
  if (n - k < zero) {
    cout << 0 << endl;
    return 0;
  }
  ll a = 0, b = 0, ans = 1;
  if (k % 2 == 1 && zero == 0 && sei.size() == 0) { // 負
    sort(all(hu));
    rep(i, k) {
      ans *= hu[i];
      ans %= mod;
    }
    cout << mod - ans << endl;
    return 0;
  }
  sort(all(sei));
  reverse(all(sei));
  sort(all(hu));
  reverse(all(hu));
  if (k % 2 == 1) {
    ll z = 0, q1 = sei.size(), w2 = hu.size();
    q1 -= 2;
    w2--;
    while ((a + b) != k - 1) {
      if (a >= q1) {
        ans *= ((hu[b] * hu[b + 1]) % mod);
        b += 2;
      } else if (b >= w2) {
        ans *= ((sei[a] * sei[a + 1]) % mod);
        a += 2;
      } else {
        ans *= max(sei[a] * sei[a + 1], hu[b] * hu[b + 1]) % mod;
        if ((sei[a] * sei[a + 1]) >= (hu[b] * hu[b + 1])) {
          a += 2;
        } else {
          b += 2;
        }
      }
      ans %= mod;
    }
    ans *= sei[a];
    ans %= mod;
    if (z == 1) {
      cout << mod - ans << endl;
    } else {
      cout << ans << endl;
    }
  } else {
    ll z = 0, q1 = sei.size(), w2 = hu.size();
    q1--;
    w2--;
    while ((a + b) != k) {
      if (a >= q1) {
        ans *= ((hu[b] * hu[b + 1]) % mod);
        b += 2;
      } else if (b >= w2) {
        ans *= ((sei[a] * sei[a + 1]) % mod);
        a += 2;
      } else {
        ans *= (max(sei[a] * sei[a + 1], hu[b] * hu[b + 1]) % mod);
        if ((sei[a] * sei[a + 1]) >= (hu[b] * hu[b + 1])) {
          a += 2;
        } else {
          b += 2;
        }
      }
      ans %= mod;
    }
    if (z == 1) {
      cout << mod - ans << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}