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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define maxn 100005
#define Mod 1000000007
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef set<int> si;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
#define __builtin_popcountll bitCountLL
#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
const int INF = 2e9;
const ll ML = 4e18;

void solve() {
  int n, k;
  cin >> n >> k;
  vll a, b;
  fori(i, 0, n) {
    ll x;
    cin >> x;
    if (x >= 0)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(all(a));
  sort(all(b));
  reverse(all(a));
  ll ans = 1;
  if (a.size() == 0 && k % 2) {
    rep(i, b.size() - 1, (int)(b.size() - k)) ans = (ans * b[i]) % Mod;
    ans = (ans + Mod) % Mod;
    cout << ans << endl;
    return;
  }
  if ((int)(a.size() + b.size()) == k) {
    for (ll i : a)
      ans = (ans * i) % Mod;
    for (ll i : b)
      ans = (ans * i) % Mod;
    ans = (ans + Mod) % Mod;
    cout << ans << endl;
    return;
  }
  bool ok = false;
  int pos = a.size() - 1;
  for (int i = 0; i * 2 <= (int)b.size(); i++) {
    if (i * 2 + (int)a.size() < k)
      continue;
    if (!ok) {
      pos = k - (2 * i) - 1;
      ok = true;
      continue;
    }
    if (pos < 1)
      break;
    if (a[pos] * a[pos - 1] > b[2 * i - 2] * b[2 * i - 1])
      break;
    pos -= 2;
  }
  fori(i, 0, pos + 1) ans = (ans * a[i]) % Mod;
  int len = k - pos - 1;
  fori(i, 0, len / 2) {
    ll x = b[i * 2] * b[i * 2 + 1] % Mod;
    ans = (ans * x) % Mod;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\\\data_in.txt", "r", stdin);
  solve();
  // cout << (-1) % 3 << endl;
  /*clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define maxn 100005
#define Mod 1000000007
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef set<int> si;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
#define __builtin_popcountll bitCountLL
#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
const int INF = 2e9;
const ll ML = 4e18;

void solve() {
  int n, k;
  cin >> n >> k;
  vll a, b;
  fori(i, 0, n) {
    ll x;
    cin >> x;
    if (x > 0)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(all(a));
  sort(all(b));
  reverse(all(a));
  ll ans = 1;
  if (a.size() == 0 && k % 2) {
    rep(i, b.size() - 1, (int)(b.size() - k)) ans = (ans * b[i]) % Mod;
    ans = (ans + Mod) % Mod;
    cout << ans << endl;
    return;
  }
  if (b.size() % 2 && (int)(a.size() + b.size()) == k) {
    for (ll i : a)
      ans = (ans * i) % Mod;
    for (ll i : b)
      ans = (ans * i) % Mod;
    ans = (ans + Mod) % Mod;
    cout << ans << endl;
  }
  bool ok = false;
  int pos = a.size() - 1;
  for (int i = 0; i * 2 <= (int)b.size(); i++) {
    if (i * 2 + (int)a.size() < k)
      continue;
    if (!ok) {
      pos = k - (2 * i) - 1;
      ok = true;
    }
    if (pos < 1)
      break;
    if (a[pos] * a[pos - 1] > b[2 * i - 2] * b[2 * i - 1])
      break;
    pos -= 2;
  }
  fori(i, 0, pos + 1) ans = (ans * a[i]) % Mod;
  int len = k - pos - 1;
  fori(i, 0, len / 2) {
    ll x = b[i * 2] * b[i * 2 + 1] % Mod;
    ans = (ans * x) % Mod;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\\\data_in.txt", "r", stdin);
  solve();
  // cout << (-1) % 3 << endl;
  /*clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/