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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define double ld
#define loop(i, n) for (int i = 0; i < (int)n; ++i)
#define loop1(i, n) for (int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define all(x) begin(x), end(x)
#define ti tuple<int, int, int>
#define Point Vect
#define no                                                                     \\
  {                                                                            \\
    cout << -1;                                                                \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  {                                                                            \\
    cout << "Yes";                                                             \\
    return;                                                                    \\
  }
#define mkp make_pair
#define mkt make_tuple
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr

const int MOD = 1e9 + 7;

void solve() {
  int n, k, ans = 1;
  cin >> n >> k;
  vector<int> a(n);
  bool mns = 1;
  loop(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      mns = 0;
  }
  sort(all(a), [](int a, int b) { return abs(a) > abs(b); });
  if (mns || n == k)
    for (int i = (k & 1 ? n - 1 : 0); (k & 1 ? i >= n - k : i < k);
         (k & 1 ? --i : ++i))
      ans = ans * (a[i] + MOD) % MOD;
  else {
    int cnt = 0;
    loop(i, k) if (a[i] < 0)++ cnt;
    if (cnt & 1) {
      int lst_pls = -1, lst_mns = -1, fst_pls = -1, fst_mns = -1;
      for (int i = k - 1; i >= 0; --i) {
        if (lst_pls == -1 && a[i] >= 0)
          lst_pls = i;
        if (lst_mns == -1 && a[i] < 0)
          lst_mns = i;
      }
      for (int i = k; i < n; ++i) {
        if (fst_pls == -1 && a[i] >= 0)
          fst_pls = i;
        if (fst_mns == -1 && a[i] < 0)
          fst_mns = i;
      }
      bool flag;
      if (fst_pls == -1 || lst_mns == -1)
        flag = 0;
      else if (fst_mns == -1 || lst_pls == -1)
        flag = 1;
      else
        flag = a[fst_pls] * a[lst_pls] > a[fst_mns] * a[lst_mns];
      loop(i, k) {
        if ((!flag && i == lst_pls) || (flag && i == lst_mns))
          continue;
        ans = ans * abs(a[i]) % MOD;
      }
      ans = ans * abs(flag ? a[fst_pls] : a[fst_mns]) % MOD;
    } else
      loop(i, k) ans = ans * abs(a[i]) % MOD;
  }
  cout << ans;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define double ld
#define loop(i, n) for (int i = 0; i < (int)n; ++i)
#define loop1(i, n) for (int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define all(x) begin(x), end(x)
#define ti tuple<int, int, int>
#define Point Vect
#define no                                                                     \\
  {                                                                            \\
    cout << -1;                                                                \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  {                                                                            \\
    cout << "Yes";                                                             \\
    return;                                                                    \\
  }
#define mkp make_pair
#define mkt make_tuple
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr

const int MOD = 1e9 + 7;

void step(int &l, int &r, int &ans, vector<int> &pls, vector<int> &mns) {
  if (l == mns.size() - 1)
    ans = ans * pls[r--] % MOD;
  else if (r == 0 || abs(mns[l]) > abs(pls[r]))
    ans = ans * (mns[l++] + MOD) % MOD;
  else
    ans = ans * pls[r--] % MOD;
}

void solve() {
  int n, k, ans = 1;
  cin >> n >> k;
  vector<int> pls, mns;
  loop(i, n) {
    int x;
    cin >> x;
    if (x > 0)
      pls.pb(x);
    else
      mns.pb(x);
  }
  sort(all(mns));
  sort(all(pls));
  if (n == k) {
    loop(i, mns.size()) ans = ans * (mns[i] + MOD) % MOD;
    loop(i, pls.size()) ans = ans * pls[i] % MOD;
  } else if (!pls.size()) {
    for (int i = (k & 1 ? n - 1 : 0); (k & 1 ? i >= n - k : i < k);
         (k & 1 ? --i : ++i))
      ans = ans * (mns[i] + MOD) % MOD;
  } else {
    if (k & 1) {
      ans = pls.back();
      pls.pop_back();
      --k;
    }
    if (k) {
      int l = 0, r = pls.size() - 1;
      for (int i = 0; i < k - 1; ++i)
        step(l, r, ans, pls, mns);
      if (l % 2 == 0)
        ans = ans * pls[r] % MOD;
      else
        ans = ans * (mns[l] + MOD) % MOD;
    }
  }
  cout << ans;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}