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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#define ll long long
#define ull unsigned long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll, ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld", &x)
#define clr(x, val) memset(x, val, sizeof(x))
#define pr(x) printf("%lld\\n", x)
#define gc getchar
#define pdd pair<dd, dd>
#define prec(x) cout << fixed << setprecision(x)
using namespace std;
ll a[200005];
ll b[100005];
ll modpow(ll a, ll n, ll tmp) {
  ll res = 1ll;
  while (n) {
    if (n % 2)
      res = (res * a) % tmp;
    a = (a * a) % tmp;
    n /= 2ll;
  }
  return res;
}
bool cmp(ll a, ll b) { return abs(a) > abs(b); }
int main() {
  ll n, k;
  ll mod = 1e9 + 7;
  boost;
  cin >> n >> k;
  ll neg = 0, ze = 0;

  for (ll i = 1; i <= n; i++)
    cin >> a[i], neg += (a[i] < 0);
  sort(a + 1, a + n + 1);

  ll ans = 1;
  if (k == n) {
    for (ll i = 1; i <= n; i++)
      ans = (ans * a[i]) % mod;
    cout << (ans + mod) % mod;
    return 0;
  }
  if (neg == n) {

    if (k % 2 == 0)
      for (ll i = 1; i <= k; i++)
        ans = (ans * a[i]) % mod;
    else {
      reverse(a + 1, a + n + 1);
      for (ll i = 1; i <= k; i++)
        ans = (ans * a[i]) % mod;
    }
    cout << (ans + mod) % mod;
    return 0;
  }

  if (k % 2 && a[n] == 0) {
    cout << 0;
    return 0;
  }

  if (k % 2 && a[n] > 0) {
    ans = a[n];
    k--;
    n--;
  }

  ll i = 1;
  ll j = n;
  ll itr = k / 2ll;

  for (ll loop = 1; loop <= itr; loop++) {
    if (a[i] * a[i + 1] > a[j] * a[j - 1]) {
      ans = (ans * a[i]) % mod;
      ans = (ans * a[i + 1]) % mod;
      i += 2;

    } else {
      ans = (ans * a[j]) % mod;
      ans = (ans * a[j - 1]) % mod;
      j -= 2;
    }
  }

  cout << ((ans + mod) % mod) << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#define ll long long
#define ull unsigned long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll, ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld", &x)
#define clr(x, val) memset(x, val, sizeof(x))
#define pr(x) printf("%lld\\n", x)
#define gc getchar
#define pdd pair<dd, dd>
#define prec(x) cout << fixed << setprecision(x)
using namespace std;
ll a[100005];
ll b[100005];
ll modpow(ll a, ll n, ll tmp) {
  ll res = 1;
  while (n) {
    if (n % 2)
      res = (res * a) % tmp;
    a = (a * a) % tmp;
    n /= 2ll;
  }
  return res;
}
bool cmp(ll a, ll b) { return abs(a) > abs(b); }
int main() {
  ll n, k;
  ll mod = 1e9 + 7;
  boost;
  cin >> n >> k;
  ll neg = 0, ze = 0;
  for (ll i = 1; i <= n; i++)
    cin >> a[i], neg += (a[i] < 0);
  sort(a + 1, a + n + 1, cmp);
  ll ans = 1;
  if (k == n) {
    for (ll i = 1; i <= n; i++)
      ans = (ans * a[i]) % mod;
    cout << (ans + mod) % mod;
    return 0;
  }
  if (neg == n) {
    reverse(a + 1, a + n + 1);
    for (ll i = 1; i <= k; i++)
      ans = (ans * a[i]) % mod;
    cout << (ans + mod) % mod;
    return 0;
  }
  ll negt = 0;
  for (ll i = 1; i <= k; i++) {
    ans = (ans * a[i]) % mod;
    if (a[i] < 0)
      negt++;
    ze += (a[i] == 0);
  }
  if (negt % 2 && ze == 0) {
    // removing lowest negative and adding next positive
    ll x1 = 0, y1 = 0;
    for (ll i = k; i >= 1; i--) {
      if (a[i] < 0) {
        x1 = a[i];
        break;
      }
    }
    for (ll i = k + 1; i <= n; i++) {
      if (a[i] >= 0) {
        y1 = a[i];
        break;
      }
    }
    // removing lowest postive and adding next negative
    ll x2 = 0, y2 = 0;

    for (ll i = k; i >= 1; i--) {
      if (a[i] > 0) {
        x2 = a[i];
        break;
      }
    }
    for (ll i = k + 1; i <= n; i++) {
      if (a[i] < 0) {
        y2 = a[i];
        break;
      }
    }

    if (x2 * y1 >= x1 * y2) {
      ans = (ans * modpow(x2, mod - 2, mod)) % mod;
      ans = (ans * y1) % mod;
    } else {
      ans = (ans * modpow(x1, mod - 2, mod)) % mod;
      ans = (ans * y2) % mod;
    }
  }
  cout << (ans + mod) % mod;
}