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

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

typedef long long int ll;
#define endl "\\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILEIO                                                                 \\
  freopen("./input.txt", "r", stdin);                                          \\
  freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \\
       blockTime.second;                                                       \\
       debug("%s : %lld ms\\n ", d,                                             \\
             chrono::duration_cast<chrono::milliseconds>(                      \\
                 chrono::high_resolution_clock::now() - blockTime.first)       \\
                 .count()),                                                    \\
            blockTime.second = false)
const int M = 1e9 + 7;

template <class T> T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) {
  return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2);
}
ll ceil2(ll a, ll b) { return (a + b - 1) / b; }

int main() {
  FASTIO
#ifdef LOCAL
  FILEIO
#endif
  int n, k;
  cin >> n >> k;
  int a[n];
  int foo;
  vi pos, neg;
  forn(i, 0, n) {
    cin >> foo;
    a[i] = foo;
    if (foo > 0)
      pos.pb(foo);
    else
      neg.pb(foo);
  }
  sort(all(pos));
  sort(all(neg));
  int nn = neg.size();
  if (pos.empty()) {
    ll ans = 1;

    for (int i = 0; i < k; ++i) {
      ll curr = 0;
      if (k & 1) {
        curr = neg[nn - 1 - i];
      } else
        curr = neg[i];
      curr %= M;
      curr += M;
      ans = (ans * curr) % M;
    }
    cout << ans << endl;
    return 0;
  }
  int pA = n - 2;
  int pB = 0;
  sort(a, a + n);

  ll ans = 1;
  if (k & 1) {
    --k;
    ans *= a[n - 1];
    --pA;
  }
  k /= 2;

  while (k) {
    --k;
    ans %= M;
    ll A = (a[pB] * (ll)a[pB + 1]);
    ll B = (a[pA] * (ll)a[pA + 1]);
    if (A > B) {
      A %= M;
      ans = (ans * A) % M;
      pB += 2;
    } else {
      B %= M;
      ans = (ans * B) % M;
      pA -= 2;
    }
    ans %= M;
  }
  ans += M;
  ans %= M;
  cout << ans << '\\n';

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long int ll;
#define endl "\\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILEIO                                                                 \\
  freopen("./input.txt", "r", stdin);                                          \\
  freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \\
       blockTime.second;                                                       \\
       debug("%s : %lld ms\\n ", d,                                             \\
             chrono::duration_cast<chrono::milliseconds>(                      \\
                 chrono::high_resolution_clock::now() - blockTime.first)       \\
                 .count()),                                                    \\
            blockTime.second = false)
const int M = 1e9 + 7;

template <class T> T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) {
  return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2);
}
ll ceil2(ll a, ll b) { return (a + b - 1) / b; }

int maxProductSubarrayOfSizeK(ll A[], int n, int k) {
  sort(A, A + n);

  ll product = 1;

  if (A[n - 1] == 0 && (k & 1))
    return 0;

  if (A[n - 1] <= 0 && (k & 1)) {
    for (int i = n - 1; i >= n - k; i--)
      product *= A[i], product %= M;
    return product;
  }
  int i = 0;

  int j = n - 1;

  if (k & 1) {
    product *= A[j];
    product %= M;
    j--;
    k--;
  }

  k >>= 1;

  for (int itr = 0; itr < k; itr++) {

    ll left_product = (A[i] * A[i + 1]) % M;

    ll right_product = (A[j] * A[j - 1]) % M;

    if (left_product > right_product) {
      product *= left_product;
      product %= M;
      i += 2;
    } else {
      product *= right_product;
      product %= M;
      j -= 2;
    }
  }

  return product;
}

int main() {
  freopen("./output.txt", "w", stdout);
  freopen("./input.txt", "r", stdin);
  int n, k;
  cin >> n >> k;
  ll arr[n];
  forn(i, 0, n) { cin >> arr[i]; }

  cout << (maxProductSubarrayOfSizeK(arr, n, k) + M) % M;

  return 0;
}