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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long A[200000];
long long MOD = 1000000007;

long long power(long long m, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = (res * m) % MOD;
    }
    m = (m * m) % MOD;
    n >>= 1;
  }
  return res;
}

long long inverse(long long a) { return power(a, MOD - 2); }

int main() {
  int N;
  int K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<long long> A_pos;
  vector<long long> A_neg;
  for (int i = 0; i < N; i++) {
    if (A[i] >= 0) {
      A_pos.push_back(A[i]);
    } else {
      A_neg.push_back(A[i]);
    }
  }

  sort(A_pos.begin(), A_pos.end());
  reverse(A_pos.begin(), A_pos.end());
  sort(A_neg.begin(), A_neg.end());

  if (A_pos.size() == 0) {
    long long ans = 1;
    if (K % 2 == 0) {
      for (int i = 0; i < K; i++) {
        ans = (ans * (MOD + A_neg[i])) % MOD;
      }
    } else {
      reverse(A_neg.begin(), A_neg.end());
      for (int i = 0; i < K; i++) {
        ans = (ans * (MOD + A_neg[i])) % MOD;
      }
    }
    cout << ans << endl;
    return 0;
  }

  int A_pos_ind = 0;
  int A_neg_ind = 0;

  vector<long long> terms;
  for (int i = 0; i < K; i++) {
    if (A_pos_ind >= A_pos.size()) {
      terms.push_back(MOD + A_neg[A_neg_ind]);
      A_neg_ind++;
    } else if (A_neg_ind >= A_neg.size()) {
      terms.push_back(A_pos[A_pos_ind]);
      A_pos_ind++;
    } else {
      if (abs(A_pos[A_pos_ind]) > abs(A_neg[A_neg_ind])) {
        terms.push_back(A_pos[A_pos_ind]);
        A_pos_ind++;
      } else {
        terms.push_back((MOD + A_neg[A_neg_ind]));
        A_neg_ind++;
      }
    }
  }

  if (A_neg_ind % 2 == 1 && K < N) {
    if (A_neg_ind >= A_neg.size() || A_pos_ind == 0) {
      for (int i = 0; i < terms.size(); i++) {
        if (terms[i] == (MOD + A_neg[A_neg_ind - 1])) {
          terms[i] = 1;
          break;
        }
      }
      terms.push_back(A_pos[A_pos_ind]);
    } else if (A_pos_ind >= A_pos.size()) {
      for (int i = 0; i < terms.size(); i++) {
        if (terms[i] == A_pos[A_pos_ind - 1]) {
          terms[i] = 1;
          break;
        }
      }
      terms.push_back(MOD + A_neg[A_neg_ind]);
    } else {
      long long tmp_pos = A_pos[A_pos_ind - 1] * A_pos[A_pos_ind];
      long long tmp_neg = A_neg[A_neg_ind - 1] * A_neg[A_neg_ind];
      if (tmp_pos > tmp_neg) {
        for (int i = 0; i < terms.size(); i++) {
          if (terms[i] == (MOD + A_neg[A_neg_ind - 1])) {
            terms[i] = 1;
            break;
          }
        }
        terms.push_back(A_pos[A_pos_ind]);
      } else {
        for (int i = 0; i < terms.size(); i++) {
          if (terms[i] == (A_pos[A_pos_ind - 1])) {
            terms[i] = 1;
            break;
          }
        }
        terms.push_back(MOD + A_neg[A_neg_ind]);
        // ans = (ans * inverse(A_pos[A_pos_ind - 1])) % MOD;
        // ans = (ans * (MOD + A_neg[A_pos_ind])) % MOD;
      }
    }
  }

  long long ans = 1;
  for (int i = 0; i < terms.size(); i++) {
    ans = (ans * terms[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long A[200000];
long long MOD = 1000000007;

long long power(long long m, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) {
      res = (res * m) % MOD;
    }
    m = (m * m) % MOD;
    n >>= 1;
  }
  return res;
}

long long inverse(long long a) { return power(a, MOD - 2); }

int main() {
  int N;
  int K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<long long> A_pos;
  vector<long long> A_neg;
  for (int i = 0; i < N; i++) {
    if (A[i] >= 0) {
      A_pos.push_back(A[i]);
    } else {
      A_neg.push_back(A[i]);
    }
  }

  sort(A_pos.begin(), A_pos.end());
  reverse(A_pos.begin(), A_pos.end());
  sort(A_neg.begin(), A_neg.end());

  if (A_pos.size() == 0) {
    long long ans = 1;
    if (K % 2 == 0) {
      for (int i = 0; i < K; i++) {
        ans = (ans * (MOD + A_neg[i])) % MOD;
      }
    } else {
      reverse(A_neg.begin(), A_neg.end());
      for (int i = 0; i < K; i++) {
        ans = (ans * (MOD + A_neg[i])) % MOD;
      }
    }
    cout << ans << endl;
    return 0;
  }

  int A_pos_ind = 0;
  int A_neg_ind = 0;

  long long ans = 1;
  for (int i = 0; i < K; i++) {
    if (A_pos_ind >= A_pos.size()) {
      ans = (ans * (MOD + A_neg[A_neg_ind])) % MOD;
      A_neg_ind++;
    } else if (A_neg_ind >= A_neg.size()) {
      ans = (ans * A_pos[A_pos_ind]) % MOD;
      A_pos_ind++;
    } else {
      if (abs(A_pos[A_pos_ind]) > abs(A_neg[A_neg_ind])) {
        ans = (ans * A_pos[A_pos_ind]) % MOD;
        A_pos_ind++;
      } else {
        ans = (ans * (MOD + A_neg[A_neg_ind])) % MOD;
        A_neg_ind++;
      }
    }
  }

  if (A_neg_ind % 2 == 0) {
    cout << ans << endl;
  } else {
    if (A_neg_ind >= A_neg.size() || A_pos_ind == 0) {
      ans = (ans * inverse(MOD + A_neg[A_neg_ind - 1])) % MOD;
      ans = (ans * A_pos[A_pos_ind]) % MOD;
      cout << ans << endl;
    } else if (A_pos_ind >= A_pos.size()) {
      ans = (ans * inverse(A_pos[A_pos_ind - 1])) % MOD;
      ans = (ans * (MOD + A_neg[A_neg_ind])) % MOD;
    } else {
      long long tmp_pos = A_pos[A_pos_ind - 1] * A_pos[A_pos_ind];
      long long tmp_neg = A_neg[A_neg_ind - 1] * A_neg[A_neg_ind];
      if (tmp_pos > tmp_neg) {
        ans = (ans * inverse(MOD + A_neg[A_neg_ind - 1])) % MOD;
        ans = (ans * A_pos[A_pos_ind]) % MOD;
      } else {
        ans = (ans * inverse(A_pos[A_pos_ind - 1])) % MOD;
        ans = (ans * (MOD + A_neg[A_pos_ind])) % MOD;
      }
      cout << ans << endl;
    }
  }
  return 0;
}