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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vl p, m;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      p.pb(x);
    } else {
      m.pb(x);
    }
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  sort(m.begin(), m.end());
  ll ans = 1;
  if (n == k) {
    rep(i, p.size()) {
      ans *= p[i];
      ans %= MOD;
    }
    rep(i, m.size()) {
      ans *= m[i];
      ans %= MOD;
    }
    cout << (MOD + ans) % MOD << endl;
    return 0;
  }
  if (p.size() == 0) {
    if (k % 2 == 0) {
      rep(i, k) {
        ans *= m[i];
        ans %= MOD;
      }
    }
    if (k % 2 == 1) {
      rep(i, k) {
        ans *= m[m.size() - i - 1];
        ans %= MOD;
      }
    }
    cout << (MOD + ans) % MOD << endl;
    return 0;
  }
  ll i = 0;
  if (k % 2 == 1) {
    ans *= p[0];
    i = 1;
  }
  vl np, nm;
  while (i + 1 < p.size()) {
    np.pb(p[i] * p[i + 1]);
    i += 2;
  }
  np.pb(0);
  i = 0;
  while (i + 1 < m.size()) {
    nm.pb(m[i] * m[i + 1]);
    i += 2;
  }
  nm.pb(0);
  k /= 2;
  ll s = 0, t = 0;
  rep(i, k) {
    ans *= max(np[s], nm[t]) % MOD;
    if (np[s] > nm[t]) {
      s++;
    } else {
      t++;
    }
    ans %= MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vl p, m;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      p.pb(x);
    } else {
      m.pb(x);
    }
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  sort(m.begin(), m.end());
  ll ans = 1;
  if (n == k) {
    rep(i, p.size()) {
      ans *= m[i];
      ans %= MOD;
    }
    rep(i, m.size()) {
      ans *= m[i];
      ans %= MOD;
    }
    cout << (MOD + ans) % MOD << endl;
  }
  if (p.size() == 0) {
    if (k % 2 == 0) {
      rep(i, k) {
        ans *= m[i];
        ans %= MOD;
      }
    }
    if (k % 2 == 1) {
      rep(i, k) {
        ans *= m[m.size() - i - 1];
        ans %= MOD;
      }
    }
    cout << (MOD + ans) % MOD << endl;
    return 0;
  }
  ll i = 0;
  if (k % 2 == 1) {
    ans *= p[0];
    i = 1;
  }
  vl np, nm;
  while (i + 1 < p.size()) {
    np.pb(p[i] * p[i + 1]);
    i += 2;
  }
  np.pb(0);
  i = 0;
  while (i + 1 < m.size()) {
    nm.pb(m[i] * m[i + 1]);
    i += 2;
  }
  nm.pb(0);
  k /= 2;
  ll s = 0, t = 0;
  rep(i, k) {
    ans *= max(np[s], nm[t]);
    if (np[s] > nm[t]) {
      s++;
    } else {
      t++;
    }
    ans %= MOD;
  }
  cout << ans << endl;
}