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

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;

  ll ans = 1, x;
  if (n == k) {
    rep(i, n) {
      cin >> x;
      ans = ans * x % MOD;
    }
    cout << (MOD + ans) % MOD << endl;
    return 0;
  }

  vector<ll> A, B;
  rep(i, n) {
    cin >> x;
    if (x >= 0)
      A.push_back(x);
    else
      B.push_back(-x);
  }

  ll a = A.size();
  ll b = B.size();
  ll i = 0, j = 0;

  sort(ALL(A));
  sort(ALL(B));
  reverse(ALL(A));
  reverse(ALL(B));

  if (a == 0 && k % 2 == 1) {
    for (int l = b - 1; l > b - 1 - k; l--) {
      ans = ans * B[l] % MOD;
    }
    cout << (MOD - ans) % MOD << endl;
    return 0;
  }
  if (a == 0 && k % 2 == 0) {
    for (int l = 0; l < k; l++) {
      ans = ans * B[l] % MOD;
    }
    cout << ans << endl;
    return 0;
  }

  if (k % 2 == 1) {
    ans = A[0];
    i++;
    k--;
  }

  while (k > 1) {
    if (a - i < 2) {
      ans = (ans * B[j] % MOD) * B[j + 1] % MOD;
      j += 2;
    } else if (b - j < 2) {
      ans = (ans * A[i] % MOD) * A[i + 1] % MOD;
      i += 2;
    } else {
      if (A[i] * A[i + 1] > B[j] * B[j + 1]) {
        ans = (ans * A[i] % MOD) * A[i + 1] % MOD;
        i += 2;
      } else {
        ans = (ans * B[j] % MOD) * B[j + 1] % MOD;
        j += 2;
      }
    }
    k -= 2;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  ll n, k;
  cin >> n >> k;

  vector<ll> A, B;
  ll x;
  rep(i, n) {
    cin >> x;
    if (x >= 0)
      A.push_back(x);
    else
      B.push_back(-x);
  }

  ll ans = 1;
  if (n == k) {
    for (auto y : A)
      ans = ans * y % MOD;
    for (auto y : B)
      ans = ans * y % MOD;
    if (B.size() % 2 == 0)
      cout << ans << endl;
    else
      cout << -ans << endl;
    return 0;
  }

  sort(ALL(A));
  sort(ALL(B));
  reverse(ALL(A));
  reverse(ALL(B));

  ll a = A.size();
  ll b = B.size();
  ll i = 0, j = 0;

  while (k > 1) {
    if (a - i < 2 && b - j < 2)
      ans = ans * A[i] % MOD * B[j] % MOD;
    else if (a - i < 2) {
      ans = ans * B[j] % MOD * B[j + 1] % MOD;
      j += 2;
    } else if (b - j < 2) {
      ans = ans * A[i] % MOD * A[i + 1] % MOD;
      i += 2;
    } else {
      if (A[i] * A[i + 1] > B[j] * B[j + 1]) {
        ans = ans * A[i] % MOD * A[i + 1] % MOD;
        i += 2;
      } else {
        ans = ans * B[j] % MOD * B[j + 1] % MOD;
        j += 2;
      }
    }
    k -= 2;
  }

  if (k == 0)
    cout << ans << endl;
  else {
    if (a - i > 0)
      cout << ans * A[i] % MOD << endl;
    else {
      ans = ans * B[j] % MOD * B[j + 1] % MOD * modinv(A[i - 1], MOD) % MOD;
      cout << ans << endl;
    }
  }

  return 0;
}