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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

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

  ll n, k;
  cin >> n >> k;

  vl plus, zero, minus;
  ll num;
  rep(i, n) {
    cin >> num;
    if (num > 0)
      plus.push_back(num);
    if (num == 0)
      zero.push_back(num);
    if (num < 0)
      minus.push_back(num);
  }

  sort(all(plus));
  sort(all(minus), greater<ll>());

  if (k > plus.size() + minus.size()) {
    cout << 0 << newl;
    return 0;
  }

  ll ans = 1;
  if (plus.size() > 0) {
    if (k % 2 == 1) {
      (ans *= plus[plus.size() - 1]) %= MOD;
      plus.pop_back();
      k--;
    }
    while (k) {
      ll num1 =
          (plus.size() >= 2 ? plus[plus.size() - 2] * plus[plus.size() - 1]
                            : 0LL);
      ll num2 =
          (minus.size() >= 2 ? minus[minus.size() - 2] * minus[minus.size() - 1]
                             : 0LL);
      if (num1 == 0 and num2 == 0)
        break;
      if (num1 >= num2) {
        (ans *= (num1 %= MOD)) %= MOD;
        plus.pop_back();
        plus.pop_back();
      } else {
        (ans *= (num2 %= MOD)) %= MOD;
        minus.pop_back();
        minus.pop_back();
      }
      k--;
      k--;
    }
    if (k and zero.size())
      ans = 0;
    else if (k) {
      (((((ans *= plus[0]) %= MOD) *= minus[0]) %= MOD) += MOD) %= MOD;
    }
    cout << ans << newl;
    return 0;
  }

  if (plus.size() == 0) {
    if (k % 2 == 0)
      rep(_, k) {
        (ans *= minus.back()) %= MOD;
        minus.pop_back();
      }
    else if (k % 2 == 1 and zero.size())
      ans = 0;
    else
      rep(i, k)(ans *= minus[i]) %= MOD;
    (ans += MOD) %= MOD;
    cout << ans << newl;
    return 0;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

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

  ll n, k;
  cin >> n >> k;

  vl plus, zero, minus;
  ll num;
  rep(i, n) {
    cin >> num;
    if (num > 0)
      plus.push_back(num);
    if (num == 0)
      zero.push_back(num);
    if (num < 0)
      minus.push_back(num);
  }

  sort(all(plus));
  sort(all(minus), greater<ll>());

  if (plus.size() + minus.size() - k < 0) {
    cout << 0 << newl;
    return 0;
  }

  ll ans = 1;
  if (plus.size() > 0) {
    if (k % 2 == 1) {
      (ans *= plus[plus.size() - 1]) %= MOD;
      plus.pop_back();
      k--;
    }
    while (k) {
      ll num1 =
          (plus.size() >= 2 ? plus[plus.size() - 2] * plus[plus.size() - 1]
                            : 0LL);
      ll num2 =
          (minus.size() >= 2 ? minus[minus.size() - 2] * minus[minus.size() - 1]
                             : 0LL);
      if (num1 == 0 and num2 == 0)
        break;
      if (num1 >= num2) {
        (ans *= (num1 %= MOD)) %= MOD;
        plus.pop_back();
        plus.pop_back();
      } else {
        (ans *= (num2 %= MOD)) %= MOD;
        minus.pop_back();
        minus.pop_back();
      }
      k--;
      k--;
    }
    if (k and zero.size())
      ans = 0;
    else if (k) {
      (((((ans *= plus[0]) %= MOD) *= minus[0]) %= MOD) += MOD) %= MOD;
    }
    cout << ans << newl;
    return 0;
  }

  if (plus.size() == 0) {
    if (k % 2 == 0)
      rep(_, k) {
        (ans *= minus.back()) %= MOD;
        minus.pop_back();
      }
    if (k % 2 == 1)
      rep(i, k)(ans *= minus[i]) %= MOD;
    (ans += MOD) %= MOD;
    cout << ans << newl;
    return 0;
  }
}