Problem p02616 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

struct mint {
  LL x;
  mint(LL x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(LL t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

LL N, K;
LL A[200000];

mint ans = 1;

void solve() {
  sort(A, A + N, [](int x, int y) { return abs(x) > abs(y); });
  vector<LL> pos, neg;
  int Np = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= 0) {
      pos.push_back(A[i]);
      if (A[i] > 0)
        Np++;
    }
    if (A[i] < 0) {
      neg.push_back(A[i]);
    }
  }

  if (Np == 0) {
    if (K % 2 == 0) {
      for (int i = 0; i < K; i++)
        ans *= mint(A[i]);
    } else {
      for (int i = N - 1; i >= N - K; i--)
        ans *= mint(A[i]);
    }
    return;
  }

  vector<LL> cand;
  if (K % 2 == 0) {
    for (int i = 0; i + 1 < pos.size(); i += 2)
      cand.push_back(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < neg.size(); i += 2)
      cand.push_back(neg[i] * neg[i + 1]);
  } else {
    ans *= mint(pos[0]);
    for (int i = 1; i + 1 < pos.size(); i += 2)
      cand.push_back(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < neg.size(); i += 2)
      cand.push_back(neg[i] * neg[i + 1]);
  }
  cand.push_back((*(--pos.end())) * (*(--neg.end())));
  sort(cand.begin(), cand.end(), greater<LL>());

  for (int i = 0; i < K / 2; i++) {
    ans *= mint(cand[i]);
    // cout << cand[i] << endl;
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans.x << endl;
  return 0;
}

Fixed submission:
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

struct mint {
  LL x;
  mint(LL x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(LL t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

LL N, K;
LL A[200000];

mint ans = 1;

void solve() {
  sort(A, A + N, [](int x, int y) { return abs(x) > abs(y); });
  vector<LL> pos, neg;
  int Np = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= 0) {
      pos.push_back(A[i]);
      if (A[i] > 0)
        Np++;
    }
    if (A[i] < 0) {
      neg.push_back(A[i]);
    }
  }

  if (Np == 0) {
    if (K % 2 == 0) {
      for (int i = 0; i < K; i++)
        ans *= mint(A[i]);
    } else {
      for (int i = N - 1; i >= N - K; i--)
        ans *= mint(A[i]);
    }
    return;
  }

  vector<LL> cand;
  if (K % 2 == 0) {
    for (int i = 0; i + 1 < pos.size(); i += 2)
      cand.push_back(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < neg.size(); i += 2)
      cand.push_back(neg[i] * neg[i + 1]);
  } else {
    ans *= mint(pos[0]);
    for (int i = 1; i + 1 < pos.size(); i += 2)
      cand.push_back(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < neg.size(); i += 2)
      cand.push_back(neg[i] * neg[i + 1]);
  }
  if (neg.size() > 0)
    cand.push_back((*(--pos.end())) * (*(--neg.end())));
  sort(cand.begin(), cand.end(), greater<LL>());

  for (int i = 0; i < K / 2; i++) {
    ans *= mint(cand[i]);
    // cout << cand[i] << endl;
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans.x << endl;
  return 0;
}