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

#include <algorithm>
#include <cstdio>

int abs(int a) { return a > 0 ? a : -a; }

bool cmp(int a, int b) { return abs(a) > abs(b); }

using std::sort;
const int mod = 1000000007l;
int n, k, a[1000010];
int pos = 0, neg = 0;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    if (a[i] >= 0)
      ++pos;
    else
      ++neg;
  }

  if (n == k) {
    long long int p = 1;
    for (int i = 1; i <= n; ++i)
      p *= (a[i] + mod) % mod, p %= mod;
    printf("%lld", (p + mod) % mod);
    return 0;
  }

  sort(a + 1, a + n + 1, cmp);

  /*
  for(int i = 1; i <= n; ++i)
  printf("%d ", a[i]);
  puts("");
  */

  if (!pos) {
    long long int p = 1;
    if (k % 2)
      for (int i = n; k; --i, --k)
        p *= a[i] + mod, p %= mod;
    else
      for (int i = 1; i <= k; ++i)
        p *= a[i] + mod, p %= mod;
    printf("%lld", (p + mod) % mod);
    return 0;
  }

  long long int ps = 0, min_ps = 1, ng = 0, max_ng = 1, pos1 = -1, pos2 = -1;
  long long int p = 1;
  for (int i = 1; i <= k; ++i) {
    if (a[i] >= 0)
      p *= min_ps, ++ps, min_ps = a[i];
    else
      p *= max_ng + mod, ++ng, max_ng = a[i];
    p %= mod;
  }

  if (ng % 2 == 0) {

    printf("%lld", (p * (max_ng + mod) % mod * min_ps % mod + mod) % mod);
    return 0;
  }

  if (ng)
    for (int i = k + 1; i <= n; ++i)
      if (a[i] >= 0) {
        pos1 = min_ps * a[i];
        break;
      }

  if (ps)
    for (int i = k + 1; i <= n; ++i)
      if (a[i] < 0) {
        pos2 = max_ng * a[i];
        break;
      }

  if (pos1 >= 0)
    printf("%lld", ((pos1 > pos2 ? pos1 : pos2) % mod * p % mod + mod) % mod);
  else
    printf("%lld", (pos2 % mod * p % mod + mod) % mod);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>

int abs(int a) { return a > 0 ? a : -a; }

bool cmp(int a, int b) { return abs(a) > abs(b); }

using std::sort;
const int mod = 1000000007l;
int n, k, a[100010];
int pos = 0, neg = 0;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    if (a[i] >= 0)
      ++pos;
    else
      ++neg;
  }

  if (n == k) {
    long long int p = 1;
    for (int i = 1; i <= n; ++i)
      p *= a[i], p %= mod;
    printf("%lld", (p + mod) % mod);
    return 0;
  }

  sort(a + 1, a + n + 1, cmp);

  if (!pos) {
    long long int p = 1;
    for (int i = n; k; --i, --k)
      p *= a[i] + mod, p %= mod;
    printf("%lld", (p + mod) % mod);
    return 0;
  }

  long long int ps = 0, min_ps = 1, ng = 0, max_ng = 1, pos1 = -1, pos2 = -1;
  long long int p = 1;
  for (int i = 1; i <= k; ++i) {
    if (a[i] >= 0)
      p *= min_ps, ++ps, min_ps = a[i];
    else
      p *= max_ng + mod, ++ng, max_ng = a[i];
    p %= mod;
  }

  if (ng % 2 == 0) {

    printf("%lld", (p * (max_ng + mod) % mod * min_ps % mod + mod) % mod);
    return 0;
  }

  for (int i = k + 1; i <= n; ++i)
    if (a[i] >= 0) {
      pos1 = min_ps * a[i];
      break;
    }

  for (int i = k + 1; i <= n; ++i)
    if (a[i] < 0) {
      pos2 = max_ng * a[i];
      break;
    }

  if (pos1 > 0)
    printf("%lld", ((pos1 > pos2 ? pos1 : pos2) % mod * p % mod) + mod % mod);
  else
    printf("%lld", (pos2 % mod * p % mod + mod) % mod);
  return 0;
}