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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)
#define mul_mod(multiplicand, multiplier, mod)                                 \\
  ((multiplicand * (multiplier % mod)) % mod)

void mul1(ll *product, ll *idx, ll a, ll *taken) {
  ll mod = 1e9 + 7;
  *product = mul_mod(*product, a, mod);
  *idx -= 1;
  *taken += 1;
}

void mul2(ll *product, ll *idx, ll a, ll b, ll *taken) {
  ll mod = 1e9 + 7;
  *product = mul_mod(*product, a * b, mod);
  *idx -= 2;
  *taken += 2;
}

int main() {
  ll n, k;
  ll mod = 1e9 + 7;
  cin >> n >> k;
  vector<ll> pos;
  vector<ll> neg;
  rep1(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp >= 0)
      pos.push_back(tmp);
    else
      neg.push_back(abs(tmp));
  }
  int pos_len = pos.size();
  int neg_len = neg.size();
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  ll product = 1;

  // obvious case
  if (neg_len == n) { // minus case
    if (k % 2 == 1) {
      rep0(i, k - 1) product = mul_mod(product, neg[i], mod);
      product = (-product + mod) % mod;
      cout << product << endl;
      return 0;
    }
  }
  if (n == k) {
    rep0(i, pos_len - 1) product = mul_mod(product, pos[i], mod);
    rep0(i, neg_len - 1) product = mul_mod(product, neg[i], mod);
    if (neg_len % 2 == 1) {
      product = (-product + mod) % mod;
    }
    cout << product << endl;
    return 0;
  }

  // not obvious case
  ll pos_idx = pos_len - 1;
  ll neg_idx = neg_len - 1;
  ll taken = 0;
  while (taken < k) {
    if (taken == 0 && (k % 2 == 1)) {
      mul1(&product, &pos_idx, pos[pos_idx], &taken);
      continue;
    }
    if (pos_idx < 0) {
      mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
    } else if (neg_idx <= 0) {
      mul1(&product, &pos_idx, pos[pos_idx], &taken);
    } else if (pos_idx == 0) {
      if (taken == k - 1)
        mul1(&product, &pos_idx, pos[pos_idx], &taken);
      else
        mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
    } else {
      if (taken == k - 1) {
        mul1(&product, &pos_idx, pos[pos_idx], &taken);
        continue;
      }
      ll pos_product = pos[pos_idx] * pos[pos_idx - 1];
      ll neg_product = neg[neg_idx] * neg[neg_idx - 1];
      if (pos_product > neg_product) {
        mul2(&product, &pos_idx, pos[pos_idx], pos[pos_idx - 1], &taken);
      } else {
        mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
      }
    }
  }
  cout << product << endl;
  return 0;
}

/*
7 3
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
-1000000000 5 3 0 0 0 0 0

955784937
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)
#define mul_mod(multiplicand, multiplier, mod)                                 \\
  ((multiplicand * (multiplier % mod)) % mod)

void mul1(ll *product, ll *idx, ll a, ll *taken) {
  ll mod = 1e9 + 7;
  *product = mul_mod(*product, a, mod);
  *idx -= 1;
  *taken += 1;
}

void mul2(ll *product, ll *idx, ll a, ll b, ll *taken) {
  ll mod = 1e9 + 7;
  *product = mul_mod(*product, a * b, mod);
  *idx -= 2;
  *taken += 2;
}

int main() {
  ll n, k;
  ll mod = 1e9 + 7;
  cin >> n >> k;
  vector<ll> pos;
  vector<ll> neg;
  rep1(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp >= 0)
      pos.push_back(tmp);
    else
      neg.push_back(abs(tmp));
  }
  int pos_len = pos.size();
  int neg_len = neg.size();
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  ll product = 1;

  // obvious case
  if (n == k) {
    rep0(i, pos_len - 1) product = mul_mod(product, pos[i], mod);
    rep0(i, neg_len - 1) product = mul_mod(product, neg[i], mod);
    if (neg_len == n) {
      if (k % 2 == 1) {
        product = (-product + mod) % mod;
      }
    }
    cout << product << endl;
    return 0;
  }

  // not obvious case
  ll pos_idx = pos_len - 1;
  ll neg_idx = neg_len - 1;
  ll taken = 0;
  while (taken < k) {
    if (taken == 0 && (k % 2 == 1)) {
      mul1(&product, &pos_idx, pos[pos_idx], &taken);
      continue;
    }
    if (pos_idx < 0) {
      mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
    } else if (neg_idx <= 0) {
      mul1(&product, &pos_idx, pos[pos_idx], &taken);
    } else {
      if (pos_idx == 0) {
        if (taken == k - 1)
          mul1(&product, &pos_idx, pos[pos_idx], &taken);
        else
          mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
      } else {
        ll pos_product = pos[pos_idx] * pos[pos_idx - 1];
        ll neg_product = neg[neg_idx] * neg[neg_idx - 1];
        if (pos_product > neg_product) {
          mul2(&product, &pos_idx, pos[pos_idx], pos[pos_idx - 1], &taken);
        } else {
          mul2(&product, &neg_idx, neg[neg_idx], neg[neg_idx - 1], &taken);
        }
      }
    }
  }
  cout << product << endl;
  return 0;
}

/*
5 5
-1 -1 -1 -3 -4
5 3
0 0 0 0 0
*/