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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll n, m;
ll a[200010], b[200010];
ll sp, sn;
bool is0 = 0;
ll md = 1e9 + 7;
ll ai[200010], bi[200010];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (!x)
      is0 = 1;
    else if (x < 0)
      b[sn++] = x;
    else
      a[sp++] = x;
  }
  sort(a, a + sp), reverse(a, a + sp);
  int ok = -1;
  for (int i = 0; i <= min(m, sn); i++) {
    if (i + sp < m)
      continue;
    if (i % 2 == 0) {
      ok = i;
      break;
    }
  }
  if (ok == -1 || sp + sn < m) {
    if (is0) {
      cout << "0";
      return 0;
    }
    vector<ll> v;
    for (int i = 0; i < sp; i++)
      v.push_back(a[i]);
    for (int i = 0; i < sn; i++)
      v.push_back(-b[i]);
    sort(v.begin(), v.end());
    ll res = 1;
    for (int i = 0; i < m; i++)
      res = (res * v[i]) % md;
    cout << (md - res) % md;
    return 0;
  }
  for (int i = 0; i < sn; i++)
    b[i] = -b[i];
  sort(b, b + sn);
  reverse(b, b + sn);
  ai[0] = bi[0] = 1;
  for (int i = 0; i < sp; i++)
    ai[i + 1] = (a[i] * ai[i]) % md;
  for (int i = 0; i < sn; i++)
    bi[i + 1] = (b[i] * bi[i]) % md;
  ll res = -1e18;
  bool fl = 0;
  for (int i = ok; i <= min(m, sn); i += 2) {
    // cout <<i<<" "<<b[i-1]*b[i-2]<<" "<<a[sp-i]*a[sp-i+1]<<" ##\\n";
    if (i == ok || b[i - 1] * b[i - 2] > a[m - i] * a[m - i + 1])
      res = (bi[i] * ai[m - i]) % md;
    else
      break;
  }
  cout << res;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll n, m;
ll a[200010], b[200010];
ll sp, sn;
bool is0 = 0;
ll md = 1e9 + 7;
ll ai[200010], bi[200010];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (!x)
      is0 = 1;
    else if (x < 0)
      b[sn++] = x;
    else
      a[sp++] = x;
  }
  sort(a, a + sp), sort(b, b + sn), reverse(b, b + sn);
  int ok = -1;
  for (int i = 0; i <= min(m, sn); i++) {
    if (i + sp < m)
      continue;
    if (i % 2 == 0) {
      ok = i;
      break;
    }
  }
  if (ok == -1) {
    vector<ll> v;
    for (int i = 0; i < sp; i++)
      v.push_back(a[i]);
    for (int i = 0; i < sn; i++)
      v.push_back(-b[i]);
    ll res = 1;
    for (int i = 0; i < m; i++)
      res = (res * v[i]) % md;
    cout << (md - res) % md;
    return 0;
  }
  for (int i = 0; i < sn; i++)
    b[i] = -b[i];
  sort(b, b + sn);
  ai[0] = bi[0] = 1;
  for (int i = 0; i < sp; i++)
    ai[i + 1] = (a[i] * ai[i]) % md;
  for (int i = 0; i < sn; i++)
    bi[i + 1] = (b[i] * bi[i]) % md;
  ll res = -1e18;
  for (int i = ok; i <= sn; i += 2) {
    // cout <<i<<" "<<b[i-1]*b[i-2]<<" "<<a[sp-i]*a[sp-i+1]<<" ##\\n";
    if (i == ok || b[i - 1] * b[i - 2] > a[sp - i] * a[sp - i + 1])
      res = (bi[i] * ai[m - i]) % md;
    else
      break;
  }
  cout << res;
  return 0;
}