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:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace __gnu_pbds;
typedef tree<pair<ll, pair<ll, ll>>, null_type, less<pair<ll, pair<ll, ll>>>,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;
#define me BOSS
#define bug() cout << "AIkahne aise" << endl
#define PI acos(-1)
/* Precode Start Here *\\
     Sieve
void sieve()
{
    for(ll i=3; i<=2000; i+=2)
    {
        for(ll j=i*i; j<=4000000; j+=i)
            A[j]=1;
    }
    v.push_back(2);
    for(ll i=3; i<=4000000; i+=2)
        if(A[i]==0)
            v.push_back(i);
}

     Power Function Modulo P

ll modPow(ll a, ll x, ll p) {
    //calculates a^x mod p in logarithmic time.
    long res = 1;
    while(x > 0) {
        if( x % 2 != 0) {
            res = (res * a) % p;
        }
        a = (a * a) % p;
        x /= 2;
    }
    return res;
}


   factorial

ll fact(ll n)
{
    ll ans=1;
    for(ll i=1;i<=n;i++)
    {
        ans=(ans*i)%mod;
    }
    return ans;
}
     Calculating nCr using Fermats law Theorem

ll modInverse(ll a, ll p) {
    //calculates the modular multiplicative of a mod m.
    //(assuming p is prime).
    return modPow(a, p-2, p);
}


ll modBinomial(ll n, ll k, ll p) {
// calculates C(n,k) mod p (assuming p is prime).

    ll numerator = 1; // n * (n-1) * ... * (n-k+1)
    for (ll i=0; i<k; i++) {
        numerator = (numerator * (n-i) ) % p;
    }

    ll denominator = 1; // k!
    for (ll i=1; i<=k; i++) {
        denominator = (denominator * i) % p;
    }

    // numerator / denominator mod p.
    return ( numerator* modInverse(denominator,p) ) % p;
}
     num of divisor and euler totient

ll phi[1000001],mark[1000001],divi[1000001];;
void divisorPhi(ll n)
{
    for(int i=1;i<=n;i++)
        phi[i]=i;
    for(int i=2;i<=n;i++)
    {
        if(!mark[i])
        {
            for(int j=i;j<=n;j+=i)
            {
                mark[j]=1;
                phi[j]*=(1-1/(double)i);
            }
        }
    }
    for(ll i=1;i<=n;i++)
    {
        for(ll j=i;j<=n;j+=i)
            divi[j]++;
    }
}
/* Precode Ends */

ll modPow(ll a, ll x, ll p) {
  // calculates a^x mod p in logarithmic time.
  long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

ll modInverse(ll a, ll p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}

#define mod 1000000007
ll A[5000001], B[5000001];
vector<ll> v;
map<ll, ll> myMap;
set<ll> mySet;
vector<pair<ll, ll>> vp;
priority_queue<ll> pq;
priority_queue<ll, vector<ll>, std::greater<ll>> first;
ll po(ll a, ll n) {
  ll ans = 1;
  for (int i = 1; i <= n; i++)
    ans *= a;
  return ans;
}
vector<ll> pos;
vector<ll> neg;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, k, a, ans = 1;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a >= 0) {
      pos.push_back(a);
    } else {
      neg.push_back(a);
    }
  }
  if (k == n) {
    for (int i = 0; i < pos.size(); i++) {
      ans *= pos[i];
      ans = (ans + mod) % mod;
    }
    for (int i = 0; i < neg.size(); i++) {
      ans *= neg[i];
      ans = (ans + mod) % mod;
    }
    cout << ans << endl;
  } else if (pos.size() == 0) {
    if (k % 2 == 1) {
      sort(neg.rbegin(), neg.rend());
    } else
      sort(neg.begin(), neg.end());
    for (int i = 0; i < k; i++) {
      ans *= neg[i];
      ans = (ans + mod) % mod;
    }
    cout << (ans + mod) % mod << endl;
  } else {
    sort(pos.rbegin(), pos.rend());
    sort(neg.begin(), neg.end());
    ll p = 0, q = 0;
    for (int i = 0; i < k; i++) {
      if (p < pos.size() && pos[p] >= abs(neg[q])) {
        ans *= pos[p];
        ans = (ans + mod) % mod;
        p++;
      } else if (q < neg.size()) {
        ans *= neg[q];
        ans = (ans + mod) % mod;
        q++;
      } else if (p < pos.size()) {
        ans *= pos[p];
        ans = (ans + mod) % mod;
        p++;
      }
    }
    if (q % 2 == 1) {
      ll d = -999999999999, t = -9999999999999;
      if (q < neg.size()) {
        d = 0;
        d -= pos[p - 1];
        d -= neg[q];
      }
      if (p < pos.size()) {
        t = 0;
        t += neg[q - 1];
        t += pos[p];
      }
      if (d < t) {
        ans *= modInverse(neg[q - 1], mod);
        ans = (ans + mod) % mod;
        ans *= pos[p];
        ans = (ans + mod) % mod;
      } else {
        ans *= modInverse(pos[p - 1], mod);
        ans = (ans + mod) % mod;
        ans *= neg[q];
        ans = (ans + mod) % mod;
      }
      cout << (ans + mod) % mod << endl;
    } else {
      cout << (ans + mod) % mod << endl;
    }
  }

  return 0;
}