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

/*
AuThOr Gwj
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr << A.FIR << " " << A.SEC << endl;
#define biggest(A, B) A = max(A, B)
#define smallest(A, B) A = min(A, B)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int n, k;
vector<LL> pos, neg, a1, a2;
const int MOD = 1e9 + 7;
int main() {
  fastio;
  cin >> n >> k;
  if (n == k) {
    LL tmp = 1;
    rb(i, 1, n) {
      LL ai;
      cin >> ai;
      tmp *= ai;
      tmp %= MOD;
      tmp += MOD;
      tmp %= MOD;
    }
    cout << tmp << endl;
    return 0;
  }
  rb(i, 1, n) {
    int ai;
    cin >> ai;
    if (ai < 0)
      neg.PB(-ai);
    else {
      pos.PB(ai);
    }
  }
  sort(ALL(pos));
  sort(ALL(neg));
  reverse(ALL(pos));
  reverse(ALL(neg));
  a1 = pos;
  a2 = neg;
  LL tmp = 1;
  rep(i, pos.size()) {
    pos[i] *= tmp;
    pos[i] %= MOD;
    tmp = pos[i];
  }
  tmp = 1;
  rep(i, neg.size()) {
    neg[i] *= tmp;
    neg[i] %= MOD;
    tmp = neg[i];
  }
  if ((k & 1) && pos.empty()) {

    reverse(ALL(a2));
    neg = a2;
    tmp = 1;
    rep(i, neg.size()) {
      neg[i] *= tmp;
      neg[i] %= MOD;
      tmp = neg[i];
    }
    cout << MOD - neg[k - 1] << endl;
    return 0; // 负数
  } else {
    // 正数
    LL res;
    bool ok = 0;
    rb(i, 0, neg.size()) {
      if (i & 1)
        continue;
      if (k - i <= pos.size()) {
        if (i && ok)
          if (a2[i - 1] * a2[i - 2] < a1[k - i] * a1[k - i + 1])
            break;
        if (i == 0) {
          res = pos[k - 1];
        } else if (k - i - 1 >= 0) {
          res = pos[k - i - 1] * neg[i - 1] % MOD;
        } else {
          res = neg[i - 1];
        }
        ok = 1;
      }
    }
    cout << res << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

/*
AuThOr Gwj
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr << A.FIR << " " << A.SEC << endl;
#define biggest(A, B) A = max(A, B)
#define smallest(A, B) A = min(A, B)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int n, k;
vector<LL> pos, neg, a1, a2;
const int MOD = 1e9 + 7;
int main() {
  fastio;
  cin >> n >> k;
  if (n == k) {
    LL tmp = 1;
    rb(i, 1, n) {
      LL ai;
      cin >> ai;
      tmp *= ai;
      tmp %= MOD;
      tmp += MOD;
      tmp %= MOD;
    }
    cout << tmp << endl;
    return 0;
  }
  rb(i, 1, n) {
    int ai;
    cin >> ai;
    if (ai < 0)
      neg.PB(-ai);
    else {
      pos.PB(ai);
    }
  }
  sort(ALL(pos));
  sort(ALL(neg));
  reverse(ALL(pos));
  reverse(ALL(neg));
  a1 = pos;
  a2 = neg;
  LL tmp = 1;
  rep(i, pos.size()) {
    pos[i] *= tmp;
    pos[i] %= MOD;
    tmp = pos[i];
  }
  tmp = 1;
  rep(i, neg.size()) {
    neg[i] *= tmp;
    neg[i] %= MOD;
    tmp = neg[i];
  }
  if ((k & 1) && pos.empty()) {

    reverse(ALL(a2));
    neg = a2;
    tmp = 1;
    rep(i, neg.size()) {
      neg[i] *= tmp;
      neg[i] %= MOD;
      tmp = neg[i];
    }
    cout << MOD - neg[k - 1] << endl;
    return 0; // 负数
  } else {
    // 正数
    LL res = pos[k - 1];
    k--;
    rb(i, 2, neg.size()) {
      if (i & 1)
        continue;
      k -= 2;
      if (k < -1)
        return 0;
      if (a2[i - 1] * a2[i - 2] < a1[k + 1] * a1[k + 2])
        break;
      if (k >= 0) {
        res = pos[k] * neg[i - 1] % MOD;
      } else {
        res = neg[i - 1];
      }
    }
    cout << res << endl;
  }
  return 0;
}