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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
/*SOME BITMASK KNOWLEDGE
1)x & (x - 1):sets the last one bit of x to zero
power of two exactly when x & (x − 1) = 0.
2)x & -x:sets all the one bits to zero, except last one bit
3)x | (x - 1):inverts all the bits after the last one bit*/
#define o_set                                                                  \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define o_setll                                                                \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set1;
typedef tree<string, null_type, less<string>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set2;
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

int main() {
  int mod = 1e9 + 7;
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  vector<ll> v1, v2;
  REP(i, 0, n) {
    cin >> arr[i];
    if (arr[i] >= 0) {
      v1.push_back(arr[i]);
    } else {
      v2.push_back(arr[i]);
    }
  }
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  if (k % 2) {
    ll p = 1;
    ll i = 0, j = 0;
    if (v1.size() > 0) {
      p *= v1[0];
      i++;
      ll ct = k - 1;
      while (ct > 0) {
        if ((i + 1 < v1.size()) && (j + 1 < v2.size())) {
          if (v1[i] * v1[i + 1] >= v2[j] * v2[j + 1]) {
            p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
            i += 2;
          } else {
            p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
            j += 2;
          }
          ct -= 2;
        } else if (i + 1 < v1.size()) {
          p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
          i += 2;
          ct -= 2;
        } else if (j + 1 < v2.size()) {
          p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
          j += 2;
          ct -= 2;
        } else {
          p = (p * ((v1[i] * v2[j]) % mod + mod)) % mod;
          i++;
          j++;
          ct -= 2;
        }
      }
    } else {
      ll pt = v2.size() - 1;
      while (k > 0) {
        p = ((p * v2[pt]) % mod + mod) % mod;
        pt--;
        k--;
      }
    }
    cout << p << endl;
  } else {
    ll i = 0, j = 0;
    ll p = 1;
    ll ct = k;
    while (ct > 0) {
      if ((i + 1 < v1.size()) && (j + 1 < v2.size())) {
        if (v1[i] * v1[i + 1] >= v2[j] * v2[j + 1]) {
          p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
          i += 2;
        } else {
          p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
          j += 2;
        }
        ct -= 2;
      } else if (i + 1 < v1.size()) {
        p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
        i += 2;
        ct -= 2;
      } else if (j + 1 < v2.size()) {
        p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
        j += 2;
        ct -= 2;
      } else {
        p = (p * ((v1[i] * v2[j]) % mod + mod)) % mod;
        i++;
        j++;
        ct -= 2;
      }
    }
    cout << p << endl;
  }
  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>
using namespace __gnu_pbds;
#define ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
/*SOME BITMASK KNOWLEDGE
1)x & (x - 1):sets the last one bit of x to zero
power of two exactly when x & (x − 1) = 0.
2)x & -x:sets all the one bits to zero, except last one bit
3)x | (x - 1):inverts all the bits after the last one bit*/
#define o_set                                                                  \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define o_setll                                                                \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set1;
typedef tree<string, null_type, less<string>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set2;
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

int main() {
  int mod = 1e9 + 7;
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  vector<ll> v1, v2;
  REP(i, 0, n) {
    cin >> arr[i];
    if (arr[i] >= 0) {
      v1.push_back(arr[i]);
    } else {
      v2.push_back(arr[i]);
    }
  }
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  if (k % 2) {
    ll p = 1;
    ll i = 0, j = 0;
    if (v1.size() > 0) {
      p *= v1[0];
      i++;
      ll ct = k - 1;
      while (ct > 0) {
        if ((i + 1 < v1.size()) && (j + 1 < v2.size())) {
          if (v1[i] * v1[i + 1] >= v2[j] * v2[j + 1]) {
            p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
            i += 2;
          } else {
            p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
            j += 2;
          }
          ct -= 2;
        } else if (i < v1.size() - 1) {
          p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
          i += 2;
          ct -= 2;
        } else if (j < v2.size() - 1) {
          p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
          j += 2;
          ct -= 2;
        } else {
          p = (p * ((v1.back() * v2.back()) % mod + mod)) % mod;
          ct -= 2;
        }
      }
    } else {
      REPI(i, v2.size() - k, v2.size()) { p = ((p * v2[i]) % mod + mod) % mod; }
    }
    cout << p << endl;
  } else {
    ll i = 0, j = 0;
    ll p = 1;
    ll ct = k;
    while (ct > 0) {
      if ((i + 1 < v1.size()) && (j + 1 < v2.size())) {
        if (v1[i] * v1[i + 1] >= v2[j] * v2[j + 1]) {
          p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
          i += 2;
        } else {
          p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
          j += 2;
        }
        ct -= 2;
      } else if (i < v1.size() - 1) {
        p = (p * ((v1[i] * v1[i + 1]) % mod)) % mod;
        i += 2;
        ct -= 2;
      } else if (j < v2.size() - 1) {
        p = (p * ((v2[j] * v2[j + 1]) % mod)) % mod;
        j += 2;
        ct -= 2;
      } else {
        p = (p * ((v1.back() * v2.back()) % mod + mod)) % mod;
        ct -= 2;
      }
    }
    cout << p << endl;
  }
  return 0;
}