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

#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#define W(x, y) cerr << "\\033[31m" << #x << " = " << x << "\\033[0m" << y;
#else
#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
// #define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acosl(-1)
#define ld long double
#define prime (1000000000 + 7)
#define INF 1000000000

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, k, res = 1;
  cin >> n >> k;
  vl pos, neg, zero;
  size_t indP = 0, indN = 0, indZ = 0;
  ll qtd = 0;
  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    if (a > 0) {
      pos.push_back(a);
    } else if (a < 0) {
      neg.push_back(a);
    } else {
      zero.push_back(a);
    }
  }

  if (min((long long)(neg.size() - neg.size() % 2), k - k % 2) + pos.size() +
          zero.size() <
      k) {
    W(1, endl)
    sort(pos.begin(), pos.end(), less<ll>());
    sort(neg.begin(), neg.end(), greater<ll>());

    while (indZ < zero.size() && qtd < k) {
      res *= zero[indZ] % prime;
      res %= prime;
      indZ += 1;
      qtd += 1;
    }
    while ((indP < pos.size() || indN < neg.size()) && qtd < k) {
      ll posV = -1;
      ll negV = -1;
      ll fV = 0;
      ll fN = 0;
      if (indP < pos.size()) {
        fV = 1;
        posV = pos[indP];
      }
      if (indN < neg.size()) {
        fN = 1;
        negV = neg[indN];
      }
      if (fV && fN) {
        if (posV > negV) {
          res *= posV % prime;
          res %= prime;
          indP += 1;
        } else {
          res *= (prime + (negV % prime)) % prime;
          res %= prime;
          indN += 1;
        }
      } else {
        if (fV) {
          res *= posV % prime;
          res %= prime;
          indP += 1;
        } else {
          res *= (prime + (negV % prime)) % prime;
          res %= prime;
          indN += 1;
        }
      }
      qtd += 1;
    }
    while (indP < pos.size() && qtd < k) {
      res *= pos[indP] % prime;
      res %= prime;
      indP += 1;
      qtd += 1;
    }
    while (indN < neg.size() && qtd < k) {
      res *= (prime + (neg[indN] % prime)) % prime;
      res %= prime;
      indN += 1;
      qtd += 1;
    }
  } else {

    sort(pos.begin(), pos.end(), greater<ll>());
    sort(neg.begin(), neg.end(), less<ll>());
    if (k % 2 == 1 && pos.size() > 0) {
      res *= pos.front();
      res %= prime;
      pos.erase(pos.begin());
      k--;
    }
    while ((indP < pos.size() - pos.size() % 2 ||
            indN < neg.size() - neg.size() % 2) &&
           qtd < k - k % 2) {
      ll posV = -1;
      ll negV = -1;
      if (indP < pos.size() - pos.size() % 2) {
        posV = pos[indP] * pos[indP + 1];
      }
      if (indN < neg.size() - neg.size() % 2) {
        negV = neg[indN] * neg[indN + 1];
      }
      if (posV > negV) {
        res *= posV % prime;
        res %= prime;
        indP += 2;
      } else {
        res *= negV % prime;
        res %= prime;
        indN += 2;
      }
      qtd += 2;
    }
    while (indP < pos.size() && qtd < k) {
      res *= pos[indP] % prime;
      res %= prime;
      indP += 1;
      qtd += 1;
    }
    while (indZ < zero.size() && qtd < k) {
      res *= zero[indZ] % prime;
      res %= prime;
      indZ += 1;
      qtd += 1;
    }
    while (indN < neg.size() && qtd < k) {
      res *= (prime + (neg[indN] % prime)) % prime;
      res %= prime;
      indN += 1;
      qtd += 1;
    }
  }
  cout << res << endl;

  return 0;
}
// 1 2 3 4 6 12
// 0	1	2	3	4	5	6	7	8	9
// 10 -	(	(	(	)	)	)	)	)	)
// - 0	1	1	1	1	1	1	0	0	0
// 0 0	1	2	3	3	3	3	3	3	3
// 3 0	0	0	0	1	2	3	3	3	3
// 3 0	0	1	2	3	3	3	3	3	3
// 3

// 6 -
// 5 - a
// 0 - ababba
// 2 - abba
// 4 - ba
// 1 - babba
// 3 - bba

// -$a
// -aa
// -aa
// -aa
// -a$

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#define W(x, y) cerr << "\\033[31m" << #x << " = " << x << "\\033[0m" << y;
#else
#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
// #define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acosl(-1)
#define ld long double
#define prime (1000000000 + 7)
#define INF 1000000000

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, k, res = 1;
  cin >> n >> k;
  vl vet(n);
  vl pos, neg, zero;
  for (int i = 0; i < n; ++i) {
    cin >> vet[i];
    if (vet[i] > 0) {
      pos.push_back(vet[i]);
    } else if (vet[i] < 0) {
      neg.push_back(vet[i]);
    } else {
      zero.push_back(vet[i]);
    }
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end(), greater<ll>());
  sort(zero.begin(), zero.end(), greater<ll>());
  size_t indP = 0, indN = 0, indZ = 0;
  ll qtd = 0;
  while ((indP < pos.size() - pos.size() % 2 ||
          indN < neg.size() - neg.size() % 2) &&
         qtd < k - k % 2) {
    ll posV = 0;
    ll negV = 0;
    if (indP < pos.size() - pos.size() % 2) {
      posV = pos[indP] * pos[indP + 1];
    }
    if (indN < neg.size() - neg.size() % 2) {
      negV = neg[indP] * neg[indP + 1];
    }
    if (posV > negV) {
      res *= posV % prime;
      res %= prime;
      indP += 2;
    } else {
      res *= negV % prime;
      res %= prime;
      indN += 2;
    }
    qtd += 2;
  }
  while (indP < pos.size() && qtd < k) {
    res *= pos[indP] % prime;
    res %= prime;
    indP += 1;
    qtd += 1;
  }
  while (indZ < zero.size() && qtd < k) {
    res *= zero[indZ] % prime;
    res %= prime;
    indZ += 1;
    qtd += 1;
  }
  while (indN < neg.size() && qtd < k) {
    res *= neg[indN] % prime;
    res %= prime;
    indN += 1;
    qtd += 1;
  }
  res = prime + res;
  res %= prime;
  cout << res << endl;

  return 0;
}
// 1 2 3 4 6 12
// 0	1	2	3	4	5	6	7	8	9
// 10 -	(	(	(	)	)	)	)	)	)
// - 0	1	1	1	1	1	1	0	0	0
// 0 0	1	2	3	3	3	3	3	3	3
// 3 0	0	0	0	1	2	3	3	3	3
// 3 0	0	1	2	3	3	3	3	3	3
// 3

// 6 -
// 5 - a
// 0 - ababba
// 2 - abba
// 4 - ba
// 1 - babba
// 3 - bba

// -$a
// -aa
// -aa
// -aa
// -a$