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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int mod = 1000000007;

bool comp(long long &a, long long &b) { return abs(a) < abs(b); }

long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

long long mod_inverse(long long x, long long mod) {
  return mod_pow(x, mod - 2, mod);
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A(N);
  vector<long long> pA, nA;
  int cnt_p = 0, cnt_n = 0, cnt_z = 0;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      cnt_z++;
    }
    if (A[i] >= 0) {
      pA.push_back(A[i]);
      cnt_p++;
    } else {
      nA.push_back(A[i]);
      cnt_n++;
    }
  }

  // 答えが負 or 0 になる場合
  if ((cnt_n == N && K % 2 == 1) || (N == K && cnt_n % 2 == 1)) {
    long long ans = 1;
    // 絶対値が小さい方からK個選ぶ
    sort(A.begin(), A.end(), comp);
    for (int i = 0; i < K; i++) {
      ans = (ans * A[i]) % mod;
    }

    ans = (ans + mod) % mod;
    cout << ans << "\\n";
    return 0;
  }

  sort(pA.begin(), pA.end(), greater<long long>());
  sort(nA.begin(), nA.end());

  // 絶対値が大きい方からK個選ぶ
  long long ans = 1;
  sort(A.rbegin(), A.rend(), comp);

  int cnt_pA = 0, cnt_nA = 0;
  for (int i = 0; i < K; i++) {
    if (A[i] >= 0) {
      cnt_pA++;
    } else {
      cnt_nA++;
    }
    ans = (ans * A[i]) % mod;
  }
  ans = (ans + mod) % mod;

  // 答えが負になるとき
  if (cnt_nA % 2 == 1) {
    if (cnt_nA == cnt_n) { // minus は全部使ってる (minus を減らすしかない)
      ans = (ans * mod_inverse(nA[cnt_nA - 1], mod)) % mod;
      ans = (ans + mod) % mod;
      ans = (ans * pA[cnt_pA]) % mod;
    } else if (cnt_pA == cnt_p) { // plus は全部使ってる
      ans = (ans * mod_inverse(pA[cnt_pA - 1], mod)) % mod;
      ans = (ans + mod) % mod;
      ans = (ans * nA[cnt_nA]) % mod;
    } else {
      if (cnt_pA == 0) {
        ans = (ans * mod_inverse(nA[cnt_nA - 1], mod)) % mod;
        ans = (ans + mod) % mod;
        ans = (ans * pA[0]) % mod;
      } else {
        long long px = pA[cnt_pA - 1], py = pA[cnt_pA];
        long long nx = nA[cnt_nA - 1], ny = nA[cnt_nA];

        if (px * py > nx * ny) {
          ans = (ans * mod_inverse(nA[cnt_nA - 1], mod)) % mod;
          ans = (ans + mod) % mod;
          ans = (ans * pA[cnt_pA]) % mod;
        } else {
          ans = (ans * mod_inverse(pA[cnt_pA - 1], mod)) % mod;
          ans = (ans + mod) % mod;
          ans = (ans * nA[cnt_nA]) % mod;
        }
      }
    }
  }

  cout << (ans + mod) % mod << "\\n";

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int mod = 1000000007;

bool comp(long long &a, long long &b) { return abs(a) < abs(b); }

long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

long long mod_inverse(long long x, long long mod) {
  return mod_pow(x, mod - 2, mod);
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A(N);
  vector<long long> pA, nA;
  int cnt_p = 0, cnt_n = 0, cnt_z = 0;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > 0) {
      pA.push_back(A[i]);
      cnt_p++;
    } else if (A[i] < 0) {
      nA.push_back(A[i]);
      cnt_n++;
    } else {
      pA.push_back(A[i]);
      cnt_z++;
    }
  }

  // 答えが負 or 0 になる場合
  if ((cnt_n == N && K % 2 == 1) || (N == K && cnt_n % 2 == 1)) {
    long long ans = 1;
    // 絶対値が小さい方からK個選ぶ
    sort(A.begin(), A.end(), comp);
    for (int i = 0; i < K; i++) {
      ans = (ans * A[i]) % mod;
    }

    ans = (ans + mod) % mod;
    cout << ans << "\\n";

    return 0;
  }

  sort(pA.begin(), pA.end(), greater<long long>());
  sort(nA.begin(), nA.end());

  // 絶対値が大きい方からK個選ぶ
  long long ans = 1;
  sort(A.rbegin(), A.rend(), comp);
  int cnt_pA = 0, cnt_nA = 0;
  for (int i = 0; i < K; i++) {
    if (A[i] >= 0) {
      cnt_pA++;
    } else {
      cnt_nA++;
    }
    ans = (ans * A[i]) % mod;
  }
  ans = (ans + mod) % mod;

  // 答えが負になるとき
  if (cnt_nA % 2 == 1) {
    ans = (ans * mod_inverse(nA[cnt_nA - 1], mod)) % mod;
  }
  ans = (ans + mod) % mod;
  ans = (ans * pA[cnt_pA]) % mod;

  cout << ans << "\\n";

  return 0;
}