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

/**
 *    created: 06.07.2020 17:35:12
 **/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const int INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

// Template for ModInt
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long x_) {
    if ((x = x_ % mod + mod) >= mod)
      x -= mod;
  }
  ModInt &operator+=(ModInt rhs) {
    if ((x += rhs.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(ModInt rhs) {
    if ((x -= rhs.x) < 0)
      x += mod;
    return *this;
  }
  ModInt &operator*=(ModInt rhs) {
    x = (unsigned long long)x * rhs.x % mod;
    return *this;
  }
  ModInt &operator/=(ModInt rhs) {
    x = (unsigned long long)x * rhs.inv().x % mod;
    return *this;
  }
  ModInt operator-() const { return -x < 0 ? mod - x : -x; }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  ModInt inv() const { return pow(*this, mod - 2); }
  friend ostream &operator<<(ostream &s, ModInt<mod> a) {
    s << a.x;
    return s;
  }
  friend istream &operator>>(istream &s, ModInt<mod> &a) {
    s >> a.x;
    return s;
  }
};

using mint = ModInt<MOD>;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> s, t;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      t.push_back(a[i]);
    else
      s.push_back(a[i]);
  }

  int S = s.size();
  int T = t.size();
  bool ok = false;
  if (S > 0) {
    if (n == k)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (k % 2 == 0);
  }

  mint ans = 1;

  if (!ok) {
    // 絶対値順でソート
    sort(all(a), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans *= a[i];
  } else {
    sort(all(s));
    sort(rall(t));
    if (k % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }
    vector<int> p;
    while (s.size() >= 2) {
      int x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      int x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      p.push_back(x);
    }
    sort(rall(p));
    rep(i, k / 2) ans *= p[i];
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

/**
 *    created: 06.07.2020 17:35:12
 **/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const int INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

// Template for ModInt
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long x_) {
    if ((x = x_ % mod + mod) >= mod)
      x -= mod;
  }
  ModInt &operator+=(ModInt rhs) {
    if ((x += rhs.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(ModInt rhs) {
    if ((x -= rhs.x) < 0)
      x += mod;
    return *this;
  }
  ModInt &operator*=(ModInt rhs) {
    x = (unsigned long long)x * rhs.x % mod;
    return *this;
  }
  ModInt &operator/=(ModInt rhs) {
    x = (unsigned long long)x * rhs.inv().x % mod;
    return *this;
  }
  ModInt operator-() const { return -x < 0 ? mod - x : -x; }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  ModInt inv() const { return pow(*this, mod - 2); }
  friend ostream &operator<<(ostream &s, ModInt<mod> a) {
    s << a.x;
    return s;
  }
  friend istream &operator>>(istream &s, ModInt<mod> &a) {
    s >> a.x;
    return s;
  }
};

using mint = ModInt<MOD>;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> s, t;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      t.push_back(a[i]);
    else
      s.push_back(a[i]);
  }

  int S = s.size();
  int T = t.size();
  bool ok = false;
  if (S > 0) {
    if (n == k)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (k % 2 == 0);
  }

  mint ans = 1;

  if (!ok) {
    // 絶対値順でソート
    sort(all(a), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans *= a[i];
  } else {
    sort(all(s));
    sort(rall(t));
    if (k % 2 == 0) {
      ans *= s.back();
      s.pop_back();
    }
    vector<int> p;
    while (s.size() >= 2) {
      int x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      int x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      p.push_back(x);
    }
    sort(rall(p));
    rep(i, k / 2) ans *= p[i];
  }
  cout << ans << endl;
  return 0;
}