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

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

ll mod = 1000000007; // 10^9 + 7

MY_NAMESPACE(testbed)

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll modval = 1000000007; // 10^9 + 7

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }

  bool neg_answer = false;
  if (pos.size() == 0)
    neg_answer = (k % 2 == 1);
  else if (n == k)
    neg_answer = (neg.size() % 2 == 1);

  mint res = 1;
  ll negres = 1;
  ll posres = 1;
  if (neg_answer) {
    sort(a.begin(), a.end(),
         [](ll x, ll y) -> bool { return abs(x) < abs(y); });
    for (int i = 0; i < k; i++) {
      res *= a[i];
      negres *= a[i];
      negres %= modval;
      negres += modval;
      negres %= modval;
    }
  } else {
    sort(pos.begin(), pos.end());   // the biggest at last
    sort(neg.rbegin(), neg.rend()); // the biggest abs(negative) at last

    if (k % 2 == 1) {
      res = pos.back(); // pos.pop_back();
      posres = pos.back();
      pos.pop_back();
    }

    vector<ll> p;
    while (pos.size() > 1) {
      ll tmp = pos.back();
      pos.pop_back();
      tmp *= pos.back();
      pos.pop_back();
      p.push_back(tmp);
    }
    while (neg.size() > 1) {
      ll tmp = neg.back();
      neg.pop_back();
      tmp *= neg.back();
      neg.pop_back();
      p.push_back(tmp);
    }
    sort(p.rbegin(), p.rend());

    for (int i = 0; i < k / 2; i++) {
      res *= p[i];
      ll pval = p[i];
      pval %= modval;
      posres *= pval;
      posres %= modval;
    }
  }

  if (neg_answer)
    cout << negres << "\\n";
  else
    cout << posres << "\\n";
  // cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_

Error: 0

Fixed submission:

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

ll mod = 1000000007; // 10^9 + 7

MY_NAMESPACE(testbed)

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll modval = 1000000007; // 10^9 + 7

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }

  bool neg_answer = false;
  if (pos.size() == 0)
    neg_answer = (k % 2 == 1);
  else if (n == k)
    neg_answer = (neg.size() % 2 == 1);

  mint res = 1;
  ll negres = 1;
  ll posres = 1;
  if (neg_answer) {
    sort(a.begin(), a.end(),
         [](ll x, ll y) -> bool { return abs(x) < abs(y); });
    for (int i = 0; i < k; i++) {
      res *= a[i];
      negres *= a[i];
      negres %= modval;
      negres += modval;
      negres %= modval;
    }
  } else {
    sort(pos.begin(), pos.end());   // the biggest at last
    sort(neg.rbegin(), neg.rend()); // the biggest abs(negative) at last

    if (k % 2 == 1) {
      res = pos.back();
      pos.pop_back();
      posres = pos.back();
      pos.pop_back();
    }

    vector<ll> p;
    while (pos.size() > 1) {
      ll tmp = pos.back();
      pos.pop_back();
      tmp *= pos.back();
      pos.pop_back();
      p.push_back(tmp);
    }
    while (neg.size() > 1) {
      ll tmp = neg.back();
      neg.pop_back();
      tmp *= neg.back();
      neg.pop_back();
      p.push_back(tmp);
    }
    sort(p.rbegin(), p.rend());

    for (int i = 0; i < k / 2; i++) {
      res *= p[i];
      ll pval = p[i];
      pval %= modval;
      posres *= pval;
      posres %= modval;
    }
  }

  if (neg_answer)
    cout << negres << "\\n";
  else
    cout << posres << "\\n";
  // cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_