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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

const llong inf = 1e18;

template <class Type_k, class Type_p>
Type_p mod_pow(long long n, Type_k k, Type_p p) { // n^k(mod p).
  if (k == 0)
    return 1;
  long long res = mod_pow(n * n % p, k / 2, p);
  if (k & 1)
    res = res * n % p;
  return res;
}

// 解説参考. 解法1.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a(n);
  int cnt = 0; // 負の数.
  REP(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      cnt++;
  }

  if (cnt == n and k % 2 == 1) { // 全て負かつkが奇数.
    // debug("debug");
    sort(ALL(a), greater<int>());
    // line(a);
    llong ans = 1;
    REP(i, k) ans = ans * (MOD + a[i]) % MOD;
    cout << ans << endl;
    return 0;
  }

  auto f = [](int x, int y) { return abs(x) > abs(y); };
  sort(ALL(a), f);
  // line(a);

  llong sum = 1;
  cnt = 0;
  REP(i, k) {
    sum = sum * a[i] % MOD;
    if (a[i] < 0)
      cnt++;
  }

  if (cnt % 2 == 0 or sum == 0 or k == n) { // 非負の場合またはk=nの場合.
    cout << (MOD + sum) % MOD << endl;
    return 0;
  }

  llong pl, pr, ml, mr;
  pl = pr = ml = mr = inf;
  REP(i, k) {
    if (a[i] < 0)
      ml = a[i];
    else
      pl = a[i];
  }
  for (int i = n - 1; i >= k; --i) {
    if (a[i] < 0)
      mr = a[i];
    else
      pr = a[i];
  }

  if (ml == inf or pr == inf)
    sum = sum * mod_pow(pl, MOD - 2, MOD) % MOD * (MOD + mr) % MOD;
  else if (pl == inf or mr == inf)
    sum = sum * mod_pow(MOD + ml, MOD - 2, MOD) % MOD * pr % MOD;
  else {
    // pr/ml>=mr/pl.
    if (pr * pl >= mr * ml)
      sum = sum * mod_pow(MOD + ml, MOD - 2, MOD) % MOD * pr % MOD;
    else
      sum = sum * mod_pow(pl, MOD - 2, MOD) % MOD * (MOD + mr) % MOD;
  }

  cout << (MOD + sum) % MOD << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

const llong inf = 1e18;

template <class Type_k, class Type_p>
Type_p mod_pow(long long n, Type_k k, Type_p p) { // n^k(mod p).
  if (k == 0)
    return 1;
  long long res = mod_pow(n * n % p, k / 2, p);
  if (k & 1)
    res = res * n % p;
  return res;
}

// 解説参考. 解法1.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a(n);
  int cnt = 0; // 負の数.
  REP(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      cnt++;
  }

  if (cnt == n and k % 2) { // 全て負かつkが奇数.
    // debug("debug");
    sort(ALL(a), greater<int>());
    // line(a);
    llong ans = 1;
    REP(i, k) ans = ans * (MOD + a[i]) % MOD;
    cout << ans << endl;
    return 0;
  }

  auto f = [](int x, int y) { return abs(x) >= abs(y); };
  sort(ALL(a), f);
  // line(a);

  llong sum = 1;
  cnt = 0;
  REP(i, k) {
    sum = sum * a[i] % MOD;
    if (a[i] < 0)
      cnt++;
  }

  if (sum >= 0 or k == n) { // 非負の場合またはk=nの場合.
    cout << (MOD + sum) % MOD << endl;
    return 0;
  }

  llong pl, pr, ml, mr;
  pl = pr = ml = mr = inf;
  REP(i, k) {
    if (a[i] < 0)
      ml = a[i];
    else
      pl = a[i];
  }
  for (int i = n - 1; i >= k; --i) {
    if (a[i] < 0)
      mr = a[i];
    else
      pr = a[i];
  }

  if (ml == inf or pr == inf)
    sum = sum * mod_pow(pl, MOD - 2, MOD) % MOD * (MOD + mr) % MOD;
  else if (pl == inf or mr == inf)
    sum = sum * mod_pow(MOD + ml, MOD - 2, MOD) % MOD * pr % MOD;
  else {
    // pr/ml>=mr/pl.
    if (pr * pl >= mr * ml)
      sum = sum * mod_pow(MOD + ml, MOD - 2, MOD) % MOD * pr % MOD;
    else
      sum = sum * mod_pow(pl, MOD - 2, MOD) % MOD * (MOD + mr) % MOD;
  }

  cout << sum << endl;
}