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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
bool comp(ll a, ll b) { return a > b; }
ll calcFinal(vector<ll> &v) {
  ll res = 1;
  for (ll i : v) {
    if (i < 0)
      res *= MOD + i;
    else
      res *= i;
    res %= MOD;
  }
  return res;
}
int main() {
  if (fopen("input.in", "r"))
    freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
  int N, K;
  cin >> N >> K;
  vector<ll> pos;
  vector<ll> neg;
  for (int i = 0; i < N; i++) {
    ll x;
    cin >> x;
    if (x < 0) {
      neg.push_back(x);
    } else {
      pos.push_back(x);
    }
  }
  sort(pos.begin(), pos.end(), comp);
  sort(neg.begin(), neg.end());
  ll negindex = 0;
  ll posindex = 0;
  int negnum = 0;
  vector<ll> v;
  ll final = 1;
  if (pos.size() == 0 && K % 2 == 1) {
    sort(neg.begin(), neg.end(), comp);
    for (int i = 0; i < K; i++) {
      final = (final * (1000000007 + neg[i])) % 1000000007;
    }
    cout << final << endl;
    return 0;
  }
  for (int i = 0; i < K; i++) {
    if (posindex == pos.size() ||
        (negindex < neg.size() && neg[negindex] * -1 > pos[posindex])) {
      v.push_back(neg[negindex]);
      negindex++;
      negnum++;
    } else {
      v.push_back(pos[posindex]);
      posindex++;
    }
  }

  int counter;
  if (K != N && negnum % 2 == 1) {
    ll add1 = -1, rem1;
    ll add2 = -1, rem2;
    if (posindex != 0 && negindex != neg.size()) {
      counter = K - 1;
      while (v[counter] < 0) {
        counter--;
      }
      add1 = negindex;
      rem1 = counter;
    }
    if (negindex != 0 && posindex != pos.size()) {
      counter = K - 1;
      while (v[counter] >= 0) {
        counter--;
      }
      add2 = posindex;
      rem2 = counter;
    }
    if (add2 == -1 ||
        (add1 != -1 && v[rem1] * pos[add2] < v[rem2] * neg[add1])) {
      v[rem1] = neg[add1];
    } else {
      v[rem2] = pos[add2];
    }
  }
  final = calcFinal(v);
  cout << final << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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

int main() {
  if (fopen("input.in", "r"))
    freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);

  long long N, K;
  cin >> N >> K;
  vector<long long> pos;
  vector<long long> neg;
  for (int i = 0; i < N; i++) {
    long long x;
    cin >> x;
    if (x < 0) {
      neg.push_back(x);
    } else {
      pos.push_back(x);
    }
  }
  sort(pos.begin(), pos.end(), comp);
  sort(neg.begin(), neg.end());
  long long final = 1;
  long long negindex = 0;
  long long posindex = 0;
  int negnum = 0;
  vector<long long> v;
  if (pos.size() == 0 && K % 2 == 1) {
    sort(neg.begin(), neg.end(), comp);
    for (int i = 0; i < K; i++) {
      final = (final * (1000000007 + neg[i])) % 1000000007;
    }
    cout << final << endl;
    return 0;
  }
  for (int i = 0; i < K; i++) {
    if (posindex == pos.size() ||
        (negindex < neg.size() && neg[negindex] * -1 > pos[posindex])) {
      // final=(final*(1000000007+neg[negindex]))%1000000007;
      v.push_back(neg[negindex]);
      negindex++;
      negnum++;
    } else {
      // final=(final*pos[posindex])%1000000007;
      v.push_back(pos[posindex]);
      posindex++;
    }
    //  cout<<final<<endl;
  }
  //  for(int i=0;i<K;i++){
  //  cout<<v[i]<<" ";
  //}
  //  cout<<endl<<final<<endl;
  int counter;
  if (K != N) {
    if (negnum % 2 == 1) {
      if (v[K - 1] < 0) {
        if (posindex == pos.size() || pos[posindex] == 0) {
          counter = K - 1;
          while (v[counter] < 0) {
            counter--;
          }
          // final=final*neg[negindex]/v[counter];
          v[counter] = neg[negindex];
        } else { // still positive left
          // final=final*pos[posindex]/v[K-1];
          v[K - 1] = pos[posindex];
        }
      } else {
        if (negindex == neg.size()) {
          counter = K - 1;
          while (v[counter] >= 0) {
            counter--;
          }
          // final=final*pos[posindex]/v[counter];
          v[counter] = pos[posindex];
        } else {
          // final=final*neg[negindex]/v[K-1];
          v[K - 1] = neg[negindex];
        }
      }
    }
  }
  for (int i = 0; i < K; i++) {
    if (v[i] < 0) {
      final = (final * (1000000007 + v[i])) % 1000000007;
    } else {
      final = (final * v[i]) % 1000000007;
    }
  }
  cout << final << endl;
}