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

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vl v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  // cout<<(-1%MOD)<<"\\n";
  sort(all(v), comp);
  ll c = 0;
  for (ll i = 0; i < k; i++) {
    if (v[i] < 0)
      c++;
  }
  if (c % 2 == 0) {
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * v[i]) % MOD;
    cout << (ans % MOD) << "\\n";
    return 0;
  }
  ll poso = -1;
  for (ll i = k; i < n; i++) {
    if (v[i] > 0) {
      poso = v[i];
      break;
    }
  }
  ll nego = 1;
  for (ll i = k; i < n; i++) {
    if (v[i] < 0) {
      nego = v[i];
      break;
    }
  }
  if (poso == -1 && c == k) {
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * v[n - i - 1]) % MOD;
    cout << (ans + MOD) % MOD << "\\n";
    return 0;
  }
  ll ans = 1;
  ll ans2 = 1;
  ll remi, remv, remv2;
  remv = 1;
  remv2 = -1;
  if (poso != -1) {

    for (ll i = k - 1; i >= 0; i--) {
      if (v[i] < 0) {
        remi = i;
        remv = v[i];
        v[i] = poso;
        break;
      }
    }
    for (ll i = 0; i < k; i++)
      ans = (ans * v[i]) % MOD;
    v[remi] = remv;
  }

  if (nego != 1) {
    for (ll i = k - 1; i >= 0; i--) {
      if (v[i] > 0) {
        remv2 = v[i];
        v[i] = nego;
        break;
      }
    }
    for (ll i = 0; i < k; i++)
      ans2 = (ans2 * v[i]) % MOD;
  }
  if ((poso == -1 || remv == 1) && (nego != 1 && remv2 != -1))
    cout << ans2 % MOD << "\\n";
  else if ((nego == 1 || remv2 == -1) && (poso != -1 && remv != 1))
    cout << ans << "\\n";
  else if ((nego != 1 && remv2 != -1) && (poso != -1 && remv != 1)) {
    if (poso * remv2 > nego * remv)
      cout << ans % MOD << "\\n";
    else
      cout << ans2 % MOD << "\\n";
  } else {
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * v[n - i - 1]) % MOD;
    cout << (ans + MOD) % MOD << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vl v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  // cout<<(-1%MOD)<<"\\n";
  sort(all(v), comp);
  ll c = 0;
  for (ll i = 0; i < k; i++) {
    if (v[i] < 0)
      c++;
  }
  if (c % 2 == 0) {
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * v[i]) % MOD;
    cout << (ans % MOD) << "\\n";
    return 0;
  }
  ll poso = -1;
  for (ll i = k; i < n; i++) {
    if (v[i] > 0) {
      poso = v[i];
      break;
    }
  }
  ll nego = 1;
  for (ll i = k; i < n; i++) {
    if (v[i] < 0) {
      nego = v[i];
      break;
    }
  }
  if (poso == -1 && c == k) {
    if (v[n - 1] == 0)
      cout << "0\\n";
    else {
      ll ans = 1;
      for (ll i = 0; i < k; i++)
        ans = (ans * v[n - i - 1]) % MOD;
      cout << (ans + MOD) % MOD << "\\n";
      return 0;
    }
  }
  ll ans = 1;
  ll ans2 = 1;
  ll remi, remv, remv2;
  if (poso != -1) {

    for (ll i = k - 1; i >= 0; i--) {
      if (v[i] < 0) {
        remi = i;
        remv = v[i];
        v[i] = poso;
        break;
      }
    }
    for (ll i = 0; i < k; i++)
      ans = (ans * v[i]) % MOD;
  }
  v[remi] = v[remv];
  if (nego != 1) {
    for (ll i = k - 1; i >= 0; i--) {
      if (v[i] > 0) {
        remv2 = v[i];
        v[i] = nego;
        break;
      }
    }
    for (ll i = 0; i < k; i++)
      ans2 = (ans2 * v[i]) % MOD;
  }
  if (poso == -1)
    cout << ans2 % MOD << "\\n";
  else if (nego == 1)
    cout << ans << "\\n";
  else {
    if (poso * remv2 > nego * remv)
      cout << ans % MOD << "\\n";
    else
      cout << ans2 % MOD << "\\n";
  }
}