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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 200005;
const ll mod = 1e9 + 7;
int n, k;
ll a[maxn], b[maxn], tot;

ll quickpow(ll a, ll x) {
  ll ret = 1;
  while (x) {
    if (x & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    x >>= 1;
  }
  return ret;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  if (k == n) {
    ll ans = 1;
    for (int i = 1; i <= n; i++)
      ans = ((ans * a[i]) % mod + mod) % mod;
    printf("%lld\\n", ((ans % mod) + mod) % mod);
    return 0;
  }
  sort(a + 1, a + n + 1);
  int fir = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i] < 0)
      b[++tot] = a[i];
  }
  fir = tot;
  for (int i = n; i >= 1; i--) {
    if (a[i] >= 0)
      b[++tot] = a[i];
  }
  ll ans = 1;
  if ((fir == n && k & 1) || (fir == 0)) {
    for (int i = 1; i <= k; i++)
      ans = ((ans * b[i]) % mod + mod) % mod;
    printf("%lld\\n", ((ans % mod) + mod) % mod);
    return 0;
  } else if (fir == n && !(k & 1)) {
    for (int i = n - k + 1; i <= n; i++)
      ans = ((ans * b[i]) % mod + mod) % mod;
    printf("%lld\\n", ((ans % mod) + mod) % mod);
    return 0;
  }
  ans = 1;
  int st = (k & 1) ? fir - k + 2 : fir - k + 1;
  if (st <= 0) {
    if (fir & 1)
      st = 2;
    else
      st = 1;
  }
  for (int i = st; i <= st + k - 1; i++)
    ans = ((ans * b[i]) % mod + mod) % mod;
  for (int i = st + k + 1; i <= n; i += 2) {
    if (b[i - k - 1] * b[i - k] > b[i - 1] * b[i])
      break;
    ll inv = quickpow((b[i - k - 1] * b[i - k]) % mod, mod - 2);
    ans = ((((ans * inv) % mod * b[i - 1]) % mod) * b[i]) % mod;
  }
  printf("%lld\\n", ((ans % mod) + mod) % mod);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 200005;
const ll mod = 1e9 + 7;
int n, k;
ll a[maxn], b[maxn], tot;

ll quickpow(ll a, ll x) {
  ll ret = 1;
  while (x) {
    if (x & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    x >>= 1;
  }
  return ret;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  if (k == n) {
    ll ans = 1;
    for (int i = 1; i <= n; i++)
      ans = ((ans * a[i]) % mod + mod) % mod;
    printf("%lld\\n", ((ans % mod) + mod) % mod);
    return 0;
  }
  sort(a + 1, a + n + 1);
  int fir = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i] < 0)
      b[++tot] = a[i];
  }
  fir = tot;
  for (int i = n; i >= 1; i--) {
    if (a[i] >= 0)
      b[++tot] = a[i];
  }
  ll ans = 1;
  for (int i = 1; i <= k; i++)
    ans = ((ans * b[i]) % mod + mod) % mod;
  if (!fir || fir == n) {
    printf("%lld\\n", ((ans % mod) + mod) % mod);
    return 0;
  }
  ans = 1;
  for (int i = fir - k + 1; i <= fir; i++)
    ans = ((ans * b[i]) % mod + mod) % mod;
  for (int i = fir + 2; i <= n; i += 2) {
    if (b[i - k - 1] * b[i - k] > b[i - 1] * b[i])
      break;
    ll inv = quickpow((b[i - k - 1] * b[i - k]) % mod, mod - 2);
    ans = ((((ans * inv) % mod * b[i - 1]) % mod) * b[i]) % mod;
  }
  printf("%lld\\n", ((ans % mod) + mod) % mod);
  return 0;
}