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

#include <bits/stdc++.h>
using namespace std;

// Template START
#define endl "\\n"
#define async                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define all(y) y.begin(), y.end()
#define present(x, y) (x.find(y) != x.end())
const int mod = (int)1e9 + 7;
#define vv(T) vector<vector<T>>
#define vvv(T) vector<vector<vector<T>>>
template <class T> auto matrix(int r, int c, T v) {
  return vector<vector<T>>(r, vector<T>(c, v));
}
template <class T> auto matrix(int o1, int o2, int o3, T v) {
  return vector<vector<vector<T>>>(o1, vector<vector<T>>(o2, vector<T>(o3, v)));
}
// Template END

int power(int x, int n) {
  if (n == 0)
    return 1;
  else if (n & 1)
    return (x * power((x * x) % mod, n / 2)) % mod;
  return power((x * x) % mod, n / 2) % mod;
}
void absorb(int &product, int &x) {
  product = (product * (x % mod)) % mod;
  if (product < 0)
    product += mod;
}

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  int ans = 1;
  if (a[n - 1] <= 0 && (k & 1)) {
    reverse(all(a));
    for (int i = 0; i < k; i++) {
      absorb(ans, a[i]);
    }
    cout << ans;
    return 0;
  }
  n--;
  // cout << k << " " << n << endl ;
  if (k & 1) {
    absorb(ans, a[n]);
    k--;
    n--;
  }
  int i = 0;
  while (k > 0) {
    int f = a[i] * a[i + 1], s = a[n] * a[n - 1];
    if (f > s) {
      f %= mod;
      absorb(ans, f);
      i += 2;
    } else {
      s %= mod;
      absorb(ans, s);
      n -= 2;
    }
    k -= 2;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// Template START
#define endl "\\n"
#define async                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define all(y) y.begin(), y.end()
#define present(x, y) (x.find(y) != x.end())
const int mod = (int)1e9 + 7;
#define vv(T) vector<vector<T>>
#define vvv(T) vector<vector<vector<T>>>
template <class T> auto matrix(int r, int c, T v) {
  return vector<vector<T>>(r, vector<T>(c, v));
}
template <class T> auto matrix(int o1, int o2, int o3, T v) {
  return vector<vector<vector<T>>>(o1, vector<vector<T>>(o2, vector<T>(o3, v)));
}
// Template END
bool compare(int a, int b) { return (abs(a) > abs(b)); }
int power(int x, int n) {
  if (n == 0)
    return 1;
  else if (n & 1)
    return (x * power((x * x) % mod, n / 2)) % mod;
  return power((x * x) % mod, n / 2) % mod;
}

signed main() {
  int overallProduct = 1;
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1), pos, neg;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    (overallProduct *= a[i]) %= mod;
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  };
  if (k == n) {
    cout << overallProduct << endl;
    return 0;
  }
  if (n == neg.size()) {
    sort(all(neg), [](int a, int b) { return a > b; });
    overallProduct = 1;
    for (int i = 0; i < k; i++) {
      (overallProduct *= neg[i]) %= mod;
    }
    cout << (overallProduct + mod) % mod << endl;
    return 0;
  }
  sort(all(pos), compare);
  sort(all(neg), compare);
  int i = 0, j = 0, posSize = pos.size(), negSize = neg.size();
  overallProduct = 1;
  while (i + j < k) {
    if (j == negSize)
      break;
    else if (i == posSize && i + j <= k - 2) {
      int pickNeg = (neg[j] * neg[j + 1]);
      overallProduct *= pickNeg;
      overallProduct %= mod;
      j += 2;
    } else if (i + j == k - 1) {
      overallProduct *= pos[i];
      overallProduct %= mod;
      i++;
    } else if (i == posSize - 1) {
      int pickNeg = (neg[j] * neg[j + 1]);
      if (pickNeg >= pos[i]) {
        overallProduct *= pickNeg;
        overallProduct %= mod;
        j += 2;
      } else {
        overallProduct *= pos[i];
        i++;
        overallProduct %= mod;
      }
    } else {
      int pickPos = (pos[i] * pos[i + 1]);
      int pickNeg = (neg[j] * neg[j + 1]);
      if (pickNeg >= pickPos) {
        overallProduct *= pos[i];
        overallProduct %= mod;
        i++;
      } else {
        overallProduct *= pickNeg;
        overallProduct %= mod;
        j += 2;
      }
    }
  }
  while (i + j < k) {
    overallProduct *= pos[i];
    i++;
    overallProduct %= mod;
  }
  cout << overallProduct << endl;

  return 0;
}