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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
typedef pair<LL, bool> P;

LL modpow(LL a, LL b) {
  LL r = 1;
  while (b) {
    if (b % 2 == 1)
      r = r * a % kMod;
    a = a * a % kMod;
    b /= 2;
  }
  return r;
}

LL invmod(LL a) { return modpow(a, kMod - 2); }

LL solve() {
  LL N, K;
  cin >> N >> K;
  vector<P> ps;
  ps.reserve(N);
  int pos_cnt = 0;
  for (int i = 0; i < N; ++i) {
    LL A;
    cin >> A;
    if (A >= 0) {
      ps.push_back({A, true});
      ++pos_cnt;
    } else {
      ps.push_back({-A, false});
    }
  }

  LL ans = 1;
  if (N == K) {
    int cnt = 0;
    for (auto [v, b] : ps) {
      ans = (ans * v) % kMod;
      if (!b)
        ++cnt;
    }
    return (cnt % 2 == 0) ? ans : -ans;
  }

  if (pos_cnt == 0 && K % 2 == 1) {
    sort(ps.begin(), ps.end());
    for (int i = 0; i < K; ++i) {
      auto [v, b] = ps[i];
      ans = (ans * v) % kMod;
    }
    return -ans;
  }

  sort(ps.rbegin(), ps.rend());
  int cnt = 0;
  LL last_pos = -1, last_neg = -1;
  for (int i = 0; i < K; ++i) {
    auto [v, b] = ps[i];
    ans = (ans * v) % kMod;
    if (b) {
      last_pos = v;
      --pos_cnt;
    } else {
      last_neg = v;
      ++cnt;
    }
  }

  if (cnt % 2 == 0) {
    return ans;
  }

  int i = K;
  LL next_neg = -1, next_pos = -1;
  while (next_neg < 0 || next_pos < 0) {
    if (i >= N)
      break;
    auto [v, b] = ps[i];
    if (b && next_pos < 0LL)
      next_pos = v;
    if (!b && next_neg < 0LL)
      next_neg = v;
    ++i;
  }

  if (last_pos < 0) {
    LL k = next_pos * invmod(last_neg) % kMod;
    return (k * ans) % kMod;
  } else if (last_neg < 0) {
    LL k = next_neg * invmod(last_pos) % kMod;
    return (k * ans) % kMod;
  } else if (next_neg >= 0 && next_pos >= 0) {
    if (next_pos * last_pos >= next_neg * last_neg) {
      LL k = next_pos * invmod(last_neg) % kMod;
      return (k * ans) % kMod;
    } else {
      LL k = next_neg * invmod(last_pos) % kMod;
      return (k * ans) % kMod;
    }
  } else if (next_neg >= 0) {
    return ((ans * next_neg) % kMod) * invmod(last_pos) % kMod;
  } else {
    return ((ans * next_pos) % kMod) * invmod(last_neg) % kMod;
  }
}

int main() {
  LL ans = solve();
  if (ans < 0)
    ans = (kMod + ans) % kMod;
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
typedef pair<LL, bool> P;

LL solve() {
  LL N, K;
  cin >> N >> K;
  vector<P> ps;
  ps.reserve(N);
  int pos_cnt = 0, neg_cnt = 0;
  for (int i = 0; i < N; ++i) {
    LL A;
    cin >> A;
    if (A >= 0) {
      ps.push_back({A, true});
      ++pos_cnt;
    } else {
      ps.push_back({-A, false});
      ++neg_cnt;
    }
  }

  LL ans = 1;
  if (N == K) {
    int cnt = 0;
    for (auto [v, b] : ps) {
      ans = (ans * v) % kMod;
      if (!b)
        ++cnt;
    }
    return (cnt % 2 == 0) ? ans : -ans;
  }

  if (pos_cnt == 0 && K % 2 == 1) {
    sort(ps.begin(), ps.end());
    for (int i = 0; i < K; ++i) {
      auto [v, b] = ps[i];
      ans = (ans * v) % kMod;
    }
    return -ans;
  }

  sort(ps.rbegin(), ps.rend());
  int cnt = 0;
  for (int i = 0; i < K; ++i) {
    auto [v, b] = ps[i];
    ans = (ans * v) % kMod;
    if (!b)
      ++cnt;
  }
  if (cnt % 2 == 0) {
    return ans;
  }
  assert(false);
}

int main() {
  LL ans = solve();
  if (ans < 0)
    ans = (kMod + ans) % kMod;
  cout << ans << endl;
}