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: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;

const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);

#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b)                                                          \\
  for (long long i = (long long)(a); i >= (long long)(b); i--)
#define ull unsigned long long
#define deb(x) cerr << #x << " = " << x << endl;
#define endl "\\n"
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define verify(x)                                                              \\
  if (x >= MOD)                                                                \\
    x -= MOD;

inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }

template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;

template <typename T> using maxpq = priority_queue<T>;
/*----------------------------------------------------------------------*/

bool cmp(ll a, ll b) { return abs(a) < abs(b); }

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, k;
  cin >> n >> k;
  vll arr(n);
  FOR(i, 0, n) cin >> arr[i];
  sort(all(arr), cmp);
  int cnt = 0, minneg = INT_MAX, minnegi = -1, minpos = INT_MAX, minposi = -1;
  FOR(i, n - k, n) {
    if (arr[i] == 0) {
      cout << 0;
      return 0;
    } else if (arr[i] < 0) {
      cnt = 1 - cnt;
      if (abs(minneg) > abs(arr[i])) {
        minneg = arr[i];
        minnegi = i;
      }
    } else {
      if (minpos > arr[i]) {
        minpos = arr[i];
        minposi = i;
      }
    }
  }

  ll res = 1;
  if (!cnt) {
    FOR(i, n - k, n) {
      res *= abs(arr[i]);
      res %= MOD;
    }
  } else {
    int posi = -1;
    RFOR(i, n - k - 1, 0) {
      if (arr[i] >= 0) {
        posi = i;
        break;
      }
    }
    int negi = -1;
    RFOR(i, n - k - 1, 0) {
      if (arr[i] <= 0) {
        negi = i;
        break;
      }
    }

    if (posi >= 0 && minnegi >= 0 && negi >= 0 && minposi >= 0) {
      // for posi. arr[posi] / arr[minnegi] > arr[negi] / arr[minposi]
      ll p = abs(arr[posi]) * 1LL * abs(arr[minposi]);
      ll n = abs(arr[negi]) * 1LL * abs(arr[minnegi]);
      if (p > n) {
        res *= arr[posi];
        res %= MOD;
        FOR(i, n - k, n) {
          if (i == minnegi)
            continue;
          res *= abs(arr[i]);
          res %= MOD;
        }
      } else {
        res *= abs(arr[negi]);
        res %= MOD;
        FOR(i, n - k, n) {
          if (i == minposi)
            continue;
          res *= abs(arr[i]);
          res %= MOD;
        }
      }
    } else if (posi >= 0 && minnegi >= 0) {
      // take last k except minnegi. instead take posi
      res *= abs(arr[posi]);
      res %= MOD;
      FOR(i, n - k, n) {
        if (i == minnegi)
          continue;
        res *= abs(arr[i]);
        res %= MOD;
      }
    } else if (negi >= 0 && minposi >= 0) {
      // take last k except minposi. instead take negi
      res *= abs(arr[negi]);
      res %= MOD;
      FOR(i, n - k, n) {
        if (i == minposi)
          continue;
        res *= abs(arr[i]);
        res %= MOD;
      }
    } else {
      FOR(i, 0, k) {
        res *= (arr[i] + MOD) % MOD;
        res %= MOD;
      }
    }
  }
  cout << res;
  return 0;
}