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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, k, o;
  vector<ll> a, b, c;
  ll z = 1;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> o;
    if (o >= 0)
      a.push_back(o);
    // else if (o == 0) b.push_back(o);
    else
      c.push_back(o);
  }
  sort(a.begin(), a.end());
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  if (n == k) {
    for (int i = 0; i < a.size(); i++)
      z *= a[i], z %= mod;
    for (int i = 0; i < c.size(); i++)
      z *= -c[i], z %= mod;
    if (c.size() % 2)
      z = (mod - z) % mod;
  } else if (a.empty() && k % 2) {
    for (int i = 0; i < k; i++)
      z = z * -c[i] % mod;
    z = (mod - z) % mod;
  } else if (a.size() == 1 && k % 2 && k + 1 == n && k > 1) {
    for (int i = 0; i < c.size(); i++)
      c[i] = -c[i];
    c.push_back(a[0]);
    sort(c.begin(), c.end());
    for (int i = 0; i < k; i++)
      z = z * c[i] % mod;
    z = (mod - z) % mod;
  } else {
    vector<pair<ll, int>> d;
    int e = 0;
    reverse(c.begin(), c.end());
    if (c.size() % 2)
      c.pop_back();
    for (ll i : a)
      d.push_back({i, 0});
    for (ll i : c)
      d.push_back({-i, 1});
    sort(d.begin(), d.end());
    reverse(d.begin(), d.end());
    for (int i = 0; i < k; i++)
      e += d[i].second;
    if (e % 2 == 0) {
      for (int i = 0; i < k; i++)
        z *= d[i].first, z %= mod;
    } else {
      int u1 = -1, v1 = -1, u2 = -1, v2 = -1;
      for (int i = k - 1; i >= 0; i--) {
        if (d[i].second) {
          u1 = i;
          break;
        }
      }
      for (int i = k; i < n; i++)
        if (!d[i].second) {
          v1 = i;
          break;
        }
      for (int i = k - 1; i >= 0; i--) {
        if (!d[i].second) {
          u2 = i;
          break;
        }
      }
      for (int i = k; i < n; i++)
        if (d[i].second) {
          v2 = i;
          break;
        }
      if ((u2 == -1 || v2 == -1) ||
          (v1 != -1 && u1 != -1 &&
           d[v1].first * d[u2].first > d[v2].first * d[u1].first)) {
        for (int i = 0; i < n; i++) {
          if (i < k) {
            if (i != u1)
              z *= d[i].first, z %= mod;
          } else {
            if (i == v1)
              z *= d[i].first, z %= mod;
          }
        }
      } else {
        for (int i = 0; i < n; i++) {
          if (i < k) {
            if (i != u2)
              z *= d[i].first, z %= mod;
          } else {
            if (i == v2)
              z *= d[i].first, z %= mod;
          }
        }
      }
    }
  }
  cout << z;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, k, o;
  vector<ll> a, b, c;
  ll z = 1;
  cin >> n >> k;
  if (n == k)
    return 1;
  for (int i = 0; i < n; i++) {
    cin >> o;
    if (o >= 0)
      a.push_back(o);
    // else if (o == 0) b.push_back(o);
    else
      c.push_back(o);
  }
  sort(a.begin(), a.end());
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  if (a.empty() && k % 2) {
    for (int i = 0; i < k; i++)
      z = z * -c[i] % mod;
    z = (mod - z) % mod;
  } else if (a.size() == 1 && k % 2 && k + 1 == n && k > 1) {
    for (int i = 0; i < c.size(); i++)
      c[i] = -c[i];
    c.push_back(a[0]);
    sort(c.begin(), c.end());
    for (int i = 0; i < k; i++)
      z = z * c[i] % mod;
    z = (mod - z) % mod;
  } else {
    vector<pair<ll, int>> d;
    int e = 0;
    reverse(c.begin(), c.end());
    if (c.size() % 2)
      c.pop_back();
    for (ll i : a)
      d.push_back({i, 0});
    for (ll i : c)
      d.push_back({-i, 1});
    sort(d.begin(), d.end());
    reverse(d.begin(), d.end());
    for (int i = 0; i < k; i++)
      e += d[i].second;
    if (e % 2 == 0) {
      for (int i = 0; i < k; i++)
        z *= d[i].first, z %= mod;
    } else {
      int u1 = -1, v1 = -1, u2 = -1, v2 = -1;
      for (int i = k - 1; i >= 0; i--) {
        if (d[i].second) {
          u1 = i;
          break;
        }
      }
      for (int i = k; i < n; i++)
        if (!d[i].second) {
          v1 = i;
          break;
        }
      for (int i = k - 1; i >= 0; i--) {
        if (!d[i].second) {
          u2 = i;
          break;
        }
      }
      for (int i = k; i < n; i++)
        if (d[i].second) {
          v2 = i;
          break;
        }
      if ((u2 == -1 || v2 == -1) ||
          (v1 != -1 && u1 != -1 &&
           d[v1].first * d[u2].first > d[v2].first * d[u1].first)) {
        for (int i = 0; i < n; i++) {
          if (i < k) {
            if (i != u1)
              z *= d[i].first, z %= mod;
          } else {
            if (i == v1)
              z *= d[i].first, z %= mod;
          }
        }
      } else {
        for (int i = 0; i < n; i++) {
          if (i < k) {
            if (i != u2)
              z *= d[i].first, z %= mod;
          } else {
            if (i == v2)
              z *= d[i].first, z %= mod;
          }
        }
      }
    }
  }
  cout << z;
}