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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep(i, a, b) for (i = a; i < b; i++)
ll int large = (ll int)1e13 + 7;
ll int mod = (ll int)1e9 + 7;
ll int const N = (ll int)1e5 + 10;

ll int mod_exp(ll int x, ll int n) {
  ll int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return result;
}

ll int mod_inverse(ll int a) { return mod_exp(a, mod - 2); }

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

ll int getMax(ll int a[], ll int n, ll int k) {

  ll int ans = 1;
  ll int pos = 0, neg = 0, zero = 0;
  for (ll int i = 0; i < n; i++) {
    if (a[i] > 0)
      pos++;
    else if (a[i] < 0)
      neg++;
    else
      zero++;
  }
  if (n == k) {
    for (ll int i = 0; i < n; i++)
      ans = (((ans * a[i]) % mod) + mod) % mod;
    return ans;
  } else if (neg == n && k % 2 == 1 && zero != 0)
    return 0;
  else if (neg == n && k % 2 == 1 && zero == 0) {
    ll int j = n - 1;
    while (k--) {
      ans = ((ans * a[j--]) % mod + mod) % mod;
    }
    return ans;
  } else {
    ll int negative = 0, pos1 = large, neg1 = large;
    for (ll int i = 0; i < k; i++) {
      ans = (ans * abs(a[i])) % mod;
      if (a[i] < 0) {
        negative++;
        neg1 = abs(a[i]);
      } else
        pos1 = a[i];
    }
    if (negative % 2 == 0)
      return ans;
    else {
      ll int neg2 = large, pos2 = large;
      for (ll int i = k; i < n; i++) {
        if (a[i] >= 0) {
          pos2 = a[i];
          break;
        }
      }
      for (ll int i = k; i < n; i++) {
        if (a[i] < 0) {
          neg2 = abs(a[i]);
          break;
        }
      }
      if (neg1 == large || pos2 == large) {
        if (neg2 == large || pos1 == large)
          return ((ans % mod) + mod) % mod;
        else {
          ans = (ans * mod_inverse(pos1)) % mod;
          ans = (ans * neg2) % mod;
          return ans;
        }
      } else if (neg2 == large || pos1 == large) {
        if (neg1 == large || pos2 == large)
          return ((ans % mod) + mod) % mod;
        else {
          ans = (ans * mod_inverse(neg1)) % mod;
          ans = (ans * pos2) % mod;
          return ans;
        }
      } else {
        if (neg1 * neg2 > pos1 * pos2) {
          ans = (ans * mod_inverse(pos1)) % mod;
          ans = (ans * neg2) % mod;
          return ans;
        } else {
          ans = (ans * mod_inverse(neg1)) % mod;
          ans = (ans * pos2) % mod;
          return ans;
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int T, i, j, ij, k1, k2;
  T = 1;
  // cin>>T;
  while (T--) {
    ll int n, k;

    cin >> n >> k;
    ll int a[n];
    rep(i, 0, n) cin >> a[i];
    sort(a, a + n, compare);
    // cout<<"ho gya"<<endl;
    cout << getMax(a, n, k);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep(i, a, b) for (i = a; i < b; i++)
ll int large = (ll int)1e13 + 7;
ll int mod = (ll int)1e9 + 7;
ll int const N = (ll int)1e5 + 10;

ll int mod_exp(ll int x, ll int n) {
  ll int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return result;
}

ll int mod_inverse(ll int a) { return mod_exp(a, mod - 2); }

bool compare(ll int a, ll int b) { return abs(a) >= abs(b); }

ll int getMax(ll int a[], ll int n, ll int k) {
  sort(a, a + n, compare);
  ll int ans = 1;
  ll int pos = 0, neg = 0, zero = 0;
  for (ll int i = 0; i < n; i++) {
    if (a[i] > 0)
      pos++;
    else if (a[i] < 0)
      neg++;
    else
      zero++;
  }
  if (n == k) {
    for (ll int i = 0; i < n; i++)
      ans = (((ans * a[i]) % mod) + mod) % mod;
    return ans;
  } else if (neg == n && k % 2 == 1 && zero != 0)
    return 0;
  else if (neg == n && k % 2 == 1 && zero == 0) {
    ll int j = n - 1;
    while (k--) {
      ans = ((ans * a[j--]) % mod + mod) % mod;
    }
    return ans;
  } else {
    ll int negative = 0, pos1 = large, neg1 = large;
    for (ll int i = 0; i < k; i++) {
      ans = (ans * abs(a[i])) % mod;
      if (a[i] < 0) {
        negative++;
        neg1 = abs(a[i]);
      } else
        pos1 = a[i];
    }
    if (negative % 2 == 0)
      return ans;
    else {
      ll int neg2 = large, pos2 = large;
      for (ll int i = k; i < n; i++) {
        if (a[i] >= 0) {
          pos2 = a[i];
          break;
        }
      }
      for (ll int i = k; i < n; i++) {
        if (a[i] < 0) {
          neg2 = abs(a[i]);
          break;
        }
      }
      if (neg1 == large || pos2 == large) {
        if (neg2 == large || pos1 == large)
          return ((ans % mod) + mod) % mod;
        else {
          ans = (ans * mod_inverse(pos1)) % mod;
          ans = (ans * neg2) % mod;
          return ans;
        }
      } else if (neg2 == large || pos1 == large) {
        if (neg1 == large || pos2 == large)
          return ((ans % mod) + mod) % mod;
        else {
          ans = (ans * mod_inverse(neg1)) % mod;
          ans = (ans * pos2) % mod;
          return ans;
        }
      } else {
        if (neg1 * neg2 > pos1 * pos2) {
          ans = (ans * mod_inverse(pos1)) % mod;
          ans = (ans * neg2) % mod;
          return ans;
        } else {
          ans = (ans * mod_inverse(neg1)) % mod;
          ans = (ans * pos2) % mod;
          return ans;
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int T, i, j, ij, k1, k2;
  T = 1;
  // cin>>T;
  while (T--) {
    ll int n, k;
    cin >> n >> k;
    ll int a[n];
    rep(i, 0, n) cin >> a[i];
    cout << getMax(a, n, k);
  }
  return 0;
}