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

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 1e9 + 7;

ll power(int x, int y) {
  if (y == 0)
    return 0;
  if (y == 1)
    return x;
  ll p = power(x, y >> 1);
  if (y & 1)
    return p * p % MOD * x % MOD;
  else
    return p * p % MOD;
}

ll inv(ll n) { return power(n, MOD - 2); }

ll N, K, A[200000];

int main() {
  cin >> N >> K;
  bool all_minus = true;
  REP(i, N) {
    cin >> A[i];
    if (A[i] > 0)
      all_minus = false;
  }
  sort(A, A + N,
       [](auto const &lhs, auto const &rhs) { return abs(lhs) > abs(rhs); });
  bool minus = false;
  ll last_minus = -1, last_plus = -1;
  ll ans = 1;
  REP(i, K) {
    if (A[i] < 0) {
      ans = ans * -A[i] % MOD;
      minus = !minus;
      last_minus = i;
    } else if (A[i] > 0) {
      ans = ans * A[i] % MOD;
      last_plus = i;
    } else {
      ans = 0;
    }
  }
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }
  if (!minus) {
    cout << ans << endl;
    return 0;
  }
  if (all_minus) {
    ans = 1;
    REP(i, K) ans = ans * -A[N - i - 1] % MOD;
    cout << (MOD - ans) % MOD << endl;
    return 0;
  }

  ll next_minus = -1, next_plus = -1;
  FOR(i, K, N) {
    if (A[i] < 0 && next_minus == -1)
      next_minus = i;
    if (A[i] > 0 && next_plus == -1)
      next_plus = i;
  }

  if (next_plus != -1 && next_minus != -1 && last_plus != -1) {
    if (A[next_plus] * A[last_plus] >= -A[next_minus] * -A[last_minus]) {
      cout << ans * inv(-A[last_minus]) % MOD * A[next_plus] % MOD << endl;
    } else {
      cout << ans * inv(A[last_plus]) % MOD * -A[next_minus] % MOD << endl;
    }
  } else if (next_plus != -1) {
    cout << ans * inv(-A[last_minus]) % MOD * A[next_plus] % MOD << endl;
  } else if (next_minus != -1 && last_plus != -1) {
    cout << ans * inv(A[last_plus]) % MOD * -A[next_minus] % MOD << endl;
  } else {
    if (N == K)
      cout << (MOD - ans) % MOD << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 1e9 + 7;

ll power(int x, int y) {
  if (y == 0)
    return 0;
  if (y == 1)
    return x;
  ll p = power(x, y >> 1);
  if (y & 1)
    return p * p % MOD * x % MOD;
  else
    return p * p % MOD;
}

ll inv(ll n) { return power(n, MOD - 2); }

ll N, K, A[200000];

int main() {
  cin >> N >> K;
  bool all_minus = true;
  REP(i, N) {
    cin >> A[i];
    if (A[i] > 0)
      all_minus = false;
  }
  sort(A, A + N,
       [](auto const &lhs, auto const &rhs) { return abs(lhs) > abs(rhs); });
  bool minus = false;
  ll last_minus = -1, last_plus = -1;
  ll ans = 1;
  REP(i, K) {
    if (A[i] < 0) {
      ans = ans * -A[i] % MOD;
      minus = !minus;
      last_minus = i;
    } else if (A[i] > 0) {
      ans = ans * A[i] % MOD;
      last_plus = i;
    } else {
      ans = 0;
    }
  }
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }
  if (!minus) {
    cout << ans << endl;
    return 0;
  }
  if (all_minus) {
    ans = 1;
    REP(i, K) ans = ans * -A[N - i - 1] % MOD;
    cout << (MOD - ans) % MOD << endl;
    return 0;
  }

  ll next_minus = -1, next_plus = -1;
  FOR(i, K, N) {
    if (A[i] < 0 && next_minus == -1)
      next_minus = i;
    if (A[i] > 0 && next_plus == -1)
      next_plus = i;
  }

  if (next_plus != -1 && next_minus != -1) {
    if (A[next_plus] * A[last_plus] > -A[next_minus] * -A[last_minus]) {
      exit(1);
      cout << ans * inv(-A[last_minus]) % MOD * A[next_plus] % MOD << endl;
    } else {
      cout << ans * inv(A[last_plus]) % MOD * (-A[next_minus]) % MOD << endl;
    }
  } else if (next_plus != -1) {
    cout << ans * inv(-A[last_minus]) % MOD * A[next_plus] % MOD << endl;
  } else if (next_minus != -1) {
    cout << ans * inv(A[last_plus]) % MOD * -A[next_minus] % MOD << endl;
  } else {
    if (N == K)
      cout << (MOD - ans) % MOD << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}