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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

int main() {
  ll n, k;
  cin >> n >> k;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll a;
      cin >> a;
      ans = (ans * a % mod + mod) % mod;
    }
    cout << ans << endl;
    return 0;
  }
  vll p, m;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      p.emplace_back(a);
    else
      m.emplace_back(a);
  }
  sort(all(p));
  reverse(all(p));
  sort(all(m));
  if (k % 2 == 1 && p.size() == 0) {
    ll ans = 1;
    for (int i = 1; i <= k; i++) {
      ans = ans * (m.at(m.size() - i) + mod) % mod;
    }
    cout << ans << endl;
  } else {
    ll posp = 0;
    ll posm = 0;
    ll ans = 1;
    if (k & 1) {
      ans = p.at(0);
      posp = 1;
      k--;
    }
    while (k) {
      ll a, b;
      if (p.size() - posp <= 1)
        a = -INF;
      else
        a = p.at(posp) * p.at(posp + 1);
      if (m.size() - posm <= 1)
        b = -INF;
      else
        b = m.at(posm) * m.at(posm + 1);
      if (a >= b) {
        ans = a % mod * ans % mod;
        posp += 2;
      } else {
        ans = b % mod * ans % mod;
        posm += 2;
      }
      k -= 2;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

int main() {
  ll n, k;
  cin >> n >> k;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll a;
      cin >> a;
      ans = (ans * a % mod + mod) % mod;
    }
    cout << ans << endl;
    return 0;
  }
  vll p, m;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      p.emplace_back(a);
    else
      m.emplace_back(a);
  }
  sort(all(p));
  reverse(all(p));
  sort(all(m));
  if (k % 2 == 1 && p.size() == 0) {
    ll ans = 1;
    for (int i = 1; i <= k; i++) {
      ans = ans * (m.at(m.size() - i) + mod) % mod;
    }
    cout << ans << endl;
  } else {
    p.emplace_back(-1);
    p.emplace_back(1);
    m.emplace_back(1);
    m.emplace_back(-1);
    ll posp = 0;
    ll posm = 0;
    ll ans = 1;
    if (k & 1) {
      ans = p.at(0);
      posp = 1;
      k--;
    }
    while (k) {
      if (p.at(posp) * p.at(posp + 1) >= m.at(posm) * m.at(posm)) {
        k -= 2;
        ans = ans * p.at(posp) % mod * p.at(posp + 1) % mod;
        posp += 2;
      } else {
        k -= 2;
        ans = ans * (m.at(posm) * m.at(posm + 1) % mod) % mod;
        posm += 2;
      }
    }
    cout << ans << endl;
  }
}