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

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

const int64_t kMod = 1000000007LL;
// const int64_t kMod = 998244353LL;
struct Mint {
  int64_t value;
  Mint(int64_t value = 0LL) : value((value % kMod + kMod) % kMod) {}
  Mint operator-() const { return Mint(-value); }
  Mint &operator+=(const Mint rhs) {
    if ((value += rhs.value) >= kMod) {
      value -= kMod;
    }
    return *this;
  }
  Mint &operator-=(const Mint rhs) {
    if ((value += kMod - rhs.value) >= kMod) {
      value -= kMod;
    }
    return *this;
  }
  Mint &operator*=(const Mint rhs) {
    (value *= rhs.value) %= kMod;
    return *this;
  }
  Mint operator+(const Mint rhs) const { return Mint(*this) += rhs; }
  Mint operator-(const Mint rhs) const { return Mint(*this) -= rhs; }
  Mint operator*(const Mint rhs) const { return Mint(*this) *= rhs; }
  Mint Pow(int64_t p) const {
    if (p == 0LL) {
      return 1LL;
    } else if (p < 0LL) {
      return Pow(-p).Pow(kMod - 2LL);
    }
    Mint temp = Pow(p >> 1LL);
    temp *= temp;
    if (p & 1LL) {
      temp *= (*this);
    }
    return temp;
  }
  Mint Inv() const { return Pow(kMod - 2LL); }
  Mint &operator/=(const Mint a) { return *this *= a.Inv(); }
  Mint operator/(const Mint a) const { return Mint(*this) /= a; }
};
std::ostream &operator<<(std::ostream &os, const Mint &a) {
  os << a.value;
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  auto GreaterThan = [](const int64_t a, const int64_t b) -> bool {
    return (abs(a) > abs(b));
  };
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  int64_t num_of_plus = 0;
  int64_t num_of_minus = 0;
  vector<int64_t> pluses;
  vector<int64_t> minuses;
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
    if (A[i] >= 0) {
      pluses.emplace_back(A[i]);
      ++num_of_plus;
    } else if (A[i] < 0) {
      minuses.emplace_back(A[i]);
      --num_of_minus;
    }
  }

  sort(A.begin(), A.end(), GreaterThan);

  Mint result = 1;
  bool can_be_plus = false;
  if (num_of_plus > 0) {
    if (N == K) {
      can_be_plus = (num_of_minus % 2 == 0);
    } else {
      can_be_plus = true;
    }
  } else if (num_of_plus == 0) {
    can_be_plus = (K % 2 == 0);
  }

  if (can_be_plus) {
    result = 1;
    sort(pluses.begin(), pluses.end());
    sort(minuses.rbegin(), minuses.rend());
    if (K % 2 == 1) {
      result *= pluses.back();
      pluses.pop_back();
      --K;
    }
    priority_queue<int64_t> q;
    while (static_cast<int64_t>(pluses.size()) >= 2) {
      int64_t tmp = pluses.back();
      pluses.pop_back();
      tmp *= pluses.back();
      pluses.pop_back();
      q.emplace(tmp);
    }
    while (static_cast<int64_t>(minuses.size()) >= 2) {
      int64_t tmp = minuses.back();
      minuses.pop_back();
      tmp *= minuses.back();
      minuses.pop_back();
      q.emplace(tmp);
    }
    while (K > 0) {
      result *= q.top();
      q.pop();
      K -= 2;
    }
  } else {
    result = 1;
    for (int64_t i = N - 1; i >= N - K; --i) {
      result *= A[i];
    }
  }
  cout << result.value << endl;
  return 0;
}


Error: 0

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

const int64_t kMod = 1000000007LL;
// const int64_t kMod = 998244353LL;

bool GreaterThan(const int64_t a, const int64_t b) {
  return (abs(a) >= abs(b));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), GreaterThan);
  int64_t result = 1;
  int32_t minus = 0;
  for (int64_t i = 0; i < K; ++i) {
    result *= A[i];
    result %= kMod;
    result += kMod;
    result %= kMod;
    if (A[i] < 0) {
      minus = (1 - minus);
    }
  }
  if (minus) {
    int64_t index_min_minus = -1;
    int64_t index_max_plus = -1;
    for (int64_t i = K - 1; i >= 0; --i) {
      if (A[i] < 0) {
        index_min_minus = i;
      }
    }
    for (int64_t i = K; i < N; ++i) {
      if (A[i] > 0) {
        index_max_plus = i;
      }
    }
    if (index_max_plus >= 0 && index_max_plus < N) {
      result = 1;
      for (int64_t i = 0; i < K; ++i) {
        if (i != index_min_minus) {
          result *= A[i];
          result += kMod;
          result %= kMod;
          result += kMod;
          result %= kMod;
        }
      }
      result *= A[index_max_plus];
      result += kMod;
      result %= kMod;
      result += kMod;
      result %= kMod;
    } else {
      result = 1;
      for (int64_t i = N - 1; i >= max(0L, N - K); --i) {
        result *= A[i];
        result += kMod;
        result %= kMod;
        result += kMod;
        result %= kMod;
      }
    }
  }
  result += kMod;
  result %= kMod;
  cout << result << endl;
  return 0;
}