Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

const ll mod = 1000000007;

ll fc(vector<pair<ll, bool>> &all, int k, int nouseindex, int add) {
  int n = all.size();
  ll ret = 1;
  rrep2(i, n - k, n) {
    if (i == nouseindex)
      continue;
    ret *= all.at(i).first;
    ret %= mod;
  }
  ret *= add;
  ret %= mod;
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> plv, miv;
  vector<pair<ll, bool>> all;
  int pl = 0, mi = 0;
  rep(i, n) {
    ll in;
    cin >> in;
    if (in >= 0) {
      all.push_back({in, true});
      pl++;
    } else {
      miv.push_back(in * -1);
      all.push_back({in * -1, false});
      mi++;
    }
  }
  sort(miv.begin(), miv.end());
  sort(all.begin(), all.end());
  if (k % 2 == 1 && pl == 0) { // 必ず負
    ll ans = 1;
    rep(i, k) {
      ans *= miv.at(i);
      ans %= mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }
  ll ans = 1;
  pl = 0, mi = 0;
  int mindex = -1, pindex = -1;
  rrep2(i, n - k, n) {
    ans *= all.at(i).first;
    ans %= mod;
    if (all.at(i).second == true) {
      pl++;
      pindex = i;
    } else {
      mi++;
      mindex = i;
    }
  }
  if (mi % 2 == 0 || n == k) {
    if (mi % 2 == 1) {
      ans = mod - ans;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  int smindex = -1, spindex = -1;
  rrep(i, n - k) {
    if (smindex == -1 && all.at(i).second == false)
      smindex = i;
    if (spindex == -1 && all.at(i).second == true)
      spindex = i;
  }
  if ((smindex == -1) ||
      (spindex != -1 && all.at(pindex).first * all.at(spindex).first >
                            all.at(mindex).first * all.at(smindex).first)) {
    ans = fc(all, k, mindex, all.at(spindex).first);
  } else {
    ans = fc(all, k, pindex, all.at(smindex).first);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

const ll mod = 1000000007;

ll fc(vector<pair<ll, bool>> &all, int k, int nouseindex, int add) {
  int n = all.size();
  ll ret = 1;
  rrep2(i, n - k, n) {
    if (i == nouseindex)
      continue;
    ret *= all.at(i).first;
    ret %= mod;
  }
  ret *= add;
  ret %= mod;
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> plv, miv;
  vector<pair<ll, bool>> all;
  int pl = 0, mi = 0;
  rep(i, n) {
    ll in;
    cin >> in;
    if (in >= 0) {
      all.push_back({in, true});
      pl++;
    } else {
      miv.push_back(in * -1);
      all.push_back({in * -1, false});
      mi++;
    }
  }
  sort(miv.begin(), miv.end());
  sort(all.begin(), all.end());
  if (k % 2 == 1 && pl == 0) { // 必ず負
    ll ans = 1;
    rep(i, k) {
      ans *= miv.at(i);
      ans %= mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }
  ll ans = 1;
  pl = 0, mi = 0;
  int mindex = -1, pindex = -1;
  rrep2(i, n - k, n) {
    ans *= all.at(i).first;
    ans %= mod;
    if (all.at(i).second == true) {
      pl++;
      pindex = i;
    } else {
      mi++;
      mindex = i;
    }
  }
  if (mi % 2 == 0 || n == k) {
    if (mi % 2 == 1) {
      ans = mod - ans;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  int smindex = -1, spindex = -1;
  rrep(i, n - k) {
    if (smindex == -1 && all.at(i).second == false)
      smindex = i;
    if (spindex == -1 && all.at(i).second == true)
      spindex = i;
  }
  if ((smindex == -1) || (pindex == -1) ||
      (spindex != -1 && all.at(pindex).first * all.at(spindex).first >
                            all.at(mindex).first * all.at(smindex).first)) {
    ans = fc(all, k, mindex, all.at(spindex).first);
  } else {
    ans = fc(all, k, pindex, all.at(smindex).first);
  }
  cout << ans << endl;
  return 0;
}