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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define MOD (ll)(1e9 + 7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs\\n", d,                                           \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll mod(ll x) {
  if (x >= 0) {
    return x % MOD;
  }
  x *= -1;
  x %= MOD;
  return (MOD - x) % MOD;
}
int main() {
  ll n, k;
  cin >> n >> k;
  deque<ll> a;
  for (int i = 0; i < n; i++) {
    ll b;
    cin >> b;
    a.pb(b);
  }
  sort(all(a));
  ll ans = 1;
  if (k % 2) {
    if (a[n - 1] >= 0) {
      ans = a[n - 1];
      k--;
      a.pop_back();
    } else {
      for (int i = 0; i < k; i++) {
        ans *= a[n - i - 1];
        ans = mod(ans);
      }
      k = 0;
    }
  }
  while (k) {
    int x = a.size();
    ll l = a[0] * a[1];
    ll r = a[x - 2] * a[x - 1];
    if (l >= r) {
      l = mod(l);
      ans *= l;
      a.pop_front();
      a.pop_front();
    } else {
      r = mod(r);
      ans *= r;
      a.pop_back();
      a.pop_back();
    }
    k -= 2;
    ans = mod(ans);
  }
  cout << ans;
}


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define MOD (ll)(1e9 + 7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs\\n", d,                                           \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll mod(ll x) {
  if (x >= 0) {
    return x % MOD;
  }
  x *= -1;
  x %= MOD;
  return (MOD - x) % MOD;
}
int main() {
  ll k, n;
  cin >> n >> k;
  vector<ll> neg, pos;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a > 0)
      pos.pb(a);
    else if (a < 0)
      neg.pb(a);
  }
  sort(all(pos), greater<int>());
  sort(all(neg));
  if (k == n) {
    ll ans = 1;
    if (pos.size() + neg.size()) {
      for (ll x : pos) {
        ans *= x;
        ans = mod(ans);
      }
      for (ll x : neg) {
        ans *= x;
        ans = mod(ans);
      }
      cout << ans;
      return 0;
    } else
      cout << 0;
    return 0;
  }
  if ((ll)(neg.size()) == n) {
    ll a = 1;
    for (int i = 0; i < k; i++) {
      a *= neg[n - i - 1];
      a = mod(a);
    }
    cout << a;
    return 0;
  }
  deque<ll> ql, qr;
  for (ll x : pos)
    ql.pb(x);
  for (ll x : neg)
    qr.pb(x);
  if (!ql.empty() && k & 1 && ql.size() % 2 == 0)
    ql.pop_back();
  if (!qr.empty() && qr.size() & 1)
    qr.pop_back();
  if (qr.size() + ql.size() < k) {
    if (neg.size() + pos.size() < k)
      cout << 0;
    else {
      ll ans = 1;
      for (ll x : pos) {
        ans *= x;
        ans = mod(ans);
      }
      for (ll x : neg) {
        ans *= x;
        ans = mod(ans);
      }
      cout << ans;
    }
    return 0;
  }
  ll ans = 1;
  while (k) {
    if (ql.empty() && !qr.empty()) {
      ans *= qr.front();
      qr.pop_front();
      ans = mod(ans);
      ans *= qr.front();
      qr.pop_front();
      ans = mod(ans);
      k -= 2;
    } else if (qr.empty() && !ql.empty()) {
      ans *= ql.front();
      ql.pop_front();
      ans = mod(ans);
      k--;
    } else {
      ll p = qr.front();
      qr.pop_front();
      ll q = qr.front();
      qr.pop_front();
      if (p * q >= ql.front()) {
        ans *= mod(p * q);
        ans = mod(ans);
        k -= 2;
      } else {
        ans *= ql.front();
        ql.pop_front();
        ans = mod(ans);
        k--;
        qr.push_front(p);
        qr.push_front(q);
      }
    }
  }
  cout << ans;
}