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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a.begin(), a.end());
  ll ans = 1, sign = 1;
  int l = 0, r = n - 1;
  if (k % 2) {
    ans = a[r--];
    --k;
    if (ans < 0)
      sign = -1;
  }
  while (k) {
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * sign > y * sign) {
      ans = x % mod * ans % mod;
      l += 2;
    } else {
      ans = y % mod * ans % mod;
      r -= 2;
    }
    k -= 2;
  }
  printf("%lld", (ans + mod) % mod);
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#if __cplusplus >= 201103L
#include <chrono>
using namespace std::chrono;
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define CASET                                                                  \\
  int ___T;                                                                    \\
  scanf("%d", &___T);                                                          \\
  for (int cs = 1; cs <= ___T; cs++)
#ifdef LOCAL
#define prln(x) (cout << #x << ' ' << x << endl)
#define pr(x) (cout << #x << ' ' << x << ' ')
#define prv(x) (cout << x << ' ')
#else
#define prln(x)
#define pr(x)
#define prv(x)
#endif
#define clr(x) memset((x), 0, sizeof((x)))
#define clr1(x) memset((x), -1, sizeof((x)))
#define endl "\\n"
#define pi acos(-1)
#define rep(i, st, ed) for (int i = (st); i <= (ed); ++i)
#define rep0(i, st, ed) for (int i = (st); i < (ed); ++i)
#define per0(i, st, ed) for (int i = (st); i > ed; --i)
#define per(i, st, ed) for (int i = (st); i >= ed; --i)
#define chmin(a, b) (a = a > b ? b : a)
#define chmax(a, b) (a = a < b ? b : a)
template <class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf() {}
template <class T, class... U> void sf(T &head, U &...tail) {
  _sf(head);
  sf(tail...);
}
template <class T> void _out(const T &x) { cout << x; }
void _out(const int &x) { printf("%d", x); }
void _out(const ll &x) { printf("%lld", x); }
void _out(const double &x) { printf("%.16f", x); }
void _out(const char &x) { putchar(x); }
void _out(const char *x) { printf("%s", x); }
template <class T, class U> void _out(const pair<T, U> &x) {
  _out(x.F);
  putchar(' ');
  _out(x.S);
}
template <class T> void _out(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _out(*i++))
    if (i != x.cbegin())
      putchar(' ');
}
void out() {}
template <class T, class... U> void out(const T &head, const U &...tail) {
  _out(head);
  putchar(sizeof...(tail) ? ' ' : '\\n');
  out(tail...);
}
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int maxn = 1e5 + 5;
int a[maxn];

int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int n, k;
  sf(n, k);
  rep(i, 0, n - 1) sf(a[i]);
  sort(a, a + n);
  int pro = 1;
  if (a[n - 1] == 0 && k % 2 == 1) {
    prln("a");
    out(0);
    return 0;
  } else if (a[n - 1] < 0 && k % 2 == 1) {
    for (int i = n - 1; i >= n - k; --i)
      pro = 1ll * pro * a[i] % mod;
    pro = (pro + mod) % mod;
    prln("b");
    out(pro);
    return 0;
  } else {
    int l = 0, r = n - 1;
    if (k % 2 == 1) {
      pro *= a[r];
      --k;
    }
    k /= 2;
    int x, y;
    for (int i = 0; i < k; ++i) {
      x = 1ll * a[l] * a[l + 1] % mod, y = 1ll * a[r] * a[r - 1] % mod;
      pr(x);
      prln(y);
      if (x > y)
        pro = 1ll * pro * x % mod, l += 2;
      else
        pro = 1ll * pro * y % mod, r -= 2;
    }
    prln(pro);
    pro = (pro + mod) % mod;
    out(pro);
  }

  return 0;
}