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

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

#define lli long long int
#define mod 1000000007

int main() {
  lli n, k, res = 1;
  cin >> n >> k;
  vector<lli> v(n);
  for (lli i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end(), greater<lli>());
  if (k == n || (v[0] < 0 && k % 2 == 1)) {
    for (lli i = 0; i < k; i++)
      res = (res * (v[i] + mod) % mod + mod) % mod;
  } else {
    lli lo = 0, hi = n - 1, cnt = 0;
    if (k % 2) {
      res = (res * v[lo]) % mod;
      cnt++;
      lo++;
    }
    while (cnt < k) {
      // if(k-cnt>=2)
      //{
      if (v[lo] * v[lo + 1] >= v[hi] * v[hi - 1])
        res = (res * (v[lo] % mod * v[lo + 1] % mod) % mod) % mod, lo = lo + 2;
      else
        res =
            (res * ((v[hi] + mod) % mod * (v[hi - 1] + mod) % mod) % mod) % mod,
        hi = hi - 2;
      cnt = cnt + 2;
      //}
      /*else
      {
              if(res*v[lo]>=res*v[hi])
              res=(res*v[lo]+mod)%mod,lo++,cnt++;
              else
              res=(res*v[hi]+mod)%mod,hi--,cnt++;
      }*/
    }
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

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

#define lli long long int
#define mod 1000000007

lli power(lli x, lli n, lli m) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    n = n >> 1;
  }
  return res;
}

lli modInverse(lli n, lli p) { return power(n, p - 2, p); }

int main() {
  lli n, k, res = 1;
  cin >> n >> k;
  vector<lli> v(n);
  for (lli i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end(), greater<lli>());
  if (k == n || (v[0] < 0 && k % 2 == 1)) {
    for (lli i = 0; i < k; i++)
      res = (res * (v[i] + mod) % mod + mod) % mod;
  } else {
    vector<lli> pos, neg;
    for (lli i = 0; i < n; i++) {
      if (v[i] <= 0)
        neg.push_back(v[i]);
      else
        pos.push_back(v[i]);
    }
    sort(pos.begin(), pos.end(), greater<lli>());
    sort(neg.begin(), neg.end());
    lli lo = 0, cnt = 0, hi = 0;
    while (cnt < k) {
      if (k - cnt >= 2) {
        if (lo + 1 < pos.size() && hi + 1 < neg.size() &&
            pos[lo] * pos[lo + 1] >= neg[hi] * neg[hi + 1])
          res = (res * (pos[lo] % mod * pos[lo + 1] % mod) % mod) % mod,
          lo += 2;
        else if (lo + 1 < pos.size() && hi + 1 < neg.size() &&
                 pos[lo] * pos[lo + 1] < neg[hi] * neg[hi + 1])
          res = (res * ((neg[hi] + mod) % mod * (neg[hi + 1] + mod) % mod) %
                 mod) %
                mod,
          hi += 2;
        else if (lo + 1 < pos.size())
          res = (res * (pos[lo] % mod * pos[lo + 1] % mod) % mod) % mod,
          lo += 2;
        else if (hi + 1 < neg.size())
          res = (res * ((neg[hi] + mod) % mod * (neg[hi + 1] + mod) % mod) %
                 mod) %
                mod,
          hi += 2;
        cnt += 2;
      } else {
        if (lo < pos.size())
          res = (res * pos[lo]) % mod;
        else {
          res = (res * modInverse(pos[lo - 1], mod)) % mod;
          res = (res * ((neg[hi] + mod) % mod * (neg[hi + 1] + mod) % mod) %
                 mod) %
                mod;
        }
        cnt++;
      }
    }
  }
  /*lli lo=0,hi=n-1,cnt=0;
  while(cnt<k)
  {
          if(k-cnt>=2)
          {
                  if(v[lo]*v[lo+1]>=v[hi]*v[hi-1])
                  res=(res*(v[lo]%mod*v[lo+1]%mod)%mod)%mod,lo=lo+2;
                  else
                  res=(res*((v[hi]+mod)%mod*(v[hi-1]+mod)%mod)%mod)%mod,hi=hi-2;
                  cnt=cnt+2;
          }
          else
          {
                  if(res*v[lo]>=res*v[hi])
                  res=(res*v[lo]+mod)%mod,lo++,cnt++;
                  else
                  res=(res*v[hi]+mod)%mod,hi--,cnt++;
          }
  }*/
  cout << res << endl;
  return 0;
}