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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
const int mod = 1e9 + 7;
int n, k;
int a[200005], maxx, minn, num = 0;
bool flags = 1;
long long ans = 1;
long long ksm(long long x, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}
bool cmp(int x, int y) { return abs(x) > abs(y); }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] >= 0)
      flags = 0;
  sort(a + 1, a + n + 1, cmp);
  if (flags) {
    if (k & 1)
      for (int i = n; i >= n - k + 1; i--)
        ans = ans * a[i] % mod;
    else
      for (int i = 1; i <= k; i++)
        ans = ans * a[i] % mod;
    ans = (ans % mod + mod) % mod;
    printf("%lld\\n", ans);
    return 0;
  }
  if (n == k) {
    ans = 1;
    for (int i = 1; i <= k; i++)
      ans = ans * a[i] % mod;
    ans = (ans % mod + mod) % mod;
    printf("%lld\\n", ans);
    return 0;
  }
  ans = 1;
  for (int i = 1; i <= k; i++) {
    ans = ans * a[i] % mod;
    if (a[i] < 0)
      num++, maxx = a[i];
    if (a[i] > 0)
      minn = a[i];
  }
  if (!ans) {
    printf("%lld\\n", ans);
    return 0;
  }
  if (!(num & 1))
    printf("%lld\\n", ans);
  else {
    long long u = 0, v = 0, u2 = 0, v2 = 0;
    v = -maxx, v2 = minn;
    for (int i = n; i > k; i--)
      if (a[i] > 0)
        u = a[i];
      else
        u2 = -a[i];
    if (!v2)
      ans = ans * ksm(-v, mod - 2) % mod * u % mod;
    else {
      if (u * v2 > u2 * v)
        ans = ans * ksm(-v, mod - 2) % mod * u % mod;
      else
        ans = ans * ksm(v2, mod - 2) % mod * -u2 % mod;
    }
    ans = (ans % mod + mod) % mod;
    printf("%lld\\n", ans);
  }
}

Error: 0

Fixed submission:

// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define fr first
#define sd second
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int n, k;
int a[N], p[N], q[N], flag;
const int mod = 1e9 + 7;
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read(), k = read();
  for (register int i = 1; i <= n; i++)
    a[i] = read();
  for (register int i = 1; i <= n; i++) {
    if (a[i] > 0)
      p[++p[0]] = a[i];
    else if (a[i] < 0)
      q[++q[0]] = -a[i];
    else
      flag = 1;
  }
  sort(p + 1, p + p[0] + 1);
  sort(q + 1, q + q[0] + 1);
  int tmp = 0;
  for (register int i = 0; i <= k; i += 2) {
    if (i <= q[0] && (k - i) <= p[0])
      tmp = 1;
  }
  if (!tmp) {
    if (flag) {
      cout << 0 << endl;
      return 0;
    }
    for (register int i = 1; i <= n; i++)
      a[i] = abs(a[i]);
    sort(a + 1, a + n + 1);
    int ans = 1;
    for (register int i = 1; i <= k; i++)
      ans = 1LL * ans * a[i] % mod;
    cout << mod - ans << endl;
    return 0;
  }
  int ans = 1;
  while (p[0] && q[0] && k) {
    if (k >= 2) {
      if (q[0] > 1 &&
          1LL * q[q[0]] * q[q[0] - 1] > 1LL * p[p[0]] * p[p[0] - 1]) {
        ans = 1LL * ans * q[q[0]] % mod * q[q[0] - 1] % mod;
        q[0] -= 2;
        k -= 2;
      } else {
        ans = 1LL * ans * p[p[0]] % mod;
        p[0]--, k--;
      }
    } else {
      ans = 1LL * ans * p[p[0]] % mod;
      p[0]--, k--;
    }
  }
  if (q[0]) {
    while (k)
      ans = 1LL * ans * q[q[0]--] % mod, k--;
  }
  if (p[0]) {
    while (k)
      ans = 1LL * ans * p[p[0]--] % mod, k--;
  }
  printf("%d\\n", ans);
  return 0;
}