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

#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
int MOD = 1000000007;
int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}
int gyaku(int n) { return modpow(n, MOD - 2); }
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int minu = 0, plu = 0, zero = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      minu++;
    else if (a[i] > 0)
      plu++;
    else
      zero++;
  }
  sort(a.begin(), a.end(),
       [](int i, int j) -> bool { return abs(i) < abs(j); });
  int ans = 1;
  if (plu == 0 && k % 2 == 1 && zero) {
    cout << 0;
    exit(0);
  }
  if ((minu % 2 == 1 && n == k) || (plu == 0 && k % 2 == 1)) {
    rep(i, k) {
      ans = ans * a[i] % MOD;
      while (ans < 0)
        ans += MOD;
    }
    while (ans < 0)
      ans += MOD;
    cout << ans << endl;
    exit(0);
  }
  rep(i, k) { ans = ans * a[n - i - 1] % MOD; }
  if (ans >= 0) {
    cout << ans;
    exit(0);
  }
  ans *= -1;
  int p = 0, q = 0, r = 0, s = 0;
  rep(i, k) {
    if (a[n - k + i] > 0) {
      p = a[n - k + i];
      break;
    }
  }
  rep(i, k) {
    if (a[n - k + i] < 0) {
      r = a[n - k + i];
      break;
    }
  }
  rep(i, n - k) {
    if (a[n - k - i - 1] < 0) {
      q = a[n - k - i - 1];
      break;
    }
  }
  rep(i, n - k) {
    if (a[n - k - i - 1] > 0) {
      s = a[n - k - i - 1];
      break;
    }
  }
  if (q * r > s * p && q != 0 && p != 0) {
    ans = (-gyaku(p) * q) % MOD * ans % MOD;
    while (ans < 0)
      ans = ans + MOD;
  } else {
    ans = (gyaku(-r) * s) % MOD * ans % MOD;
    while (ans < 0)
      ans = ans + MOD;
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
int MOD = 1000000007;
int modpow(int a, int x) {
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}
int gyaku(int n) { return modpow(n, MOD - 2); }
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int minu = 0, plu = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      minu++;
    else
      plu++;
  }

  sort(a.begin(), a.end(),
       [](int i, int j) -> bool { return abs(i) < abs(j); });
  int ans = 1;

  if ((minu % 2 == 1 && n == k) || (plu == 0 && k % 2 == 1)) {
    rep(i, k) { ans = ans * a[i] % MOD; }
    if (ans < 0)
      ans += MOD;
    cout << ans << endl;
    exit(0);
  }
  rep(i, k) { ans = ans * a[n - i - 1] % MOD; }
  if (ans > 0) {
    cout << ans;
    exit(0);
  }
  int p, q, r, s;
  rep(i, k) {
    if (a[n - k + i] > 0) {
      p = a[n - k + i];
      break;
    }
  }
  rep(i, k) {
    if (a[n - k + i] < 0) {
      r = a[n - k + i];
      break;
    }
  }
  rep(i, n - k) {
    if (a[n - k - i - 1] < 0) {
      q = a[n - k - i - 1];
      break;
    }
  }
  rep(i, n - k) {
    if (a[n - k - i - 1] > 0) {
      s = a[n - k - i - 1];
      break;
    }
  }
  if (q / p * (-1) > s / r * (-1)) {
    ans = gyaku(p) * q * ans % MOD;
  } else
    ans = gyaku(r) * s * ans % MOD;
  cout << ans << endl;
}