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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long modpow(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
long long modinv(long long a) { return modpow(a, MOD - 2); }
int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int zero = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0) {
      zero++;
    }
  }
  if (zero > N - K) {
    cout << 0 << endl;
  } else {
    vector<pair<long long, bool>> B;
    for (int i = 0; i < N; i++) {
      if (A[i] > 0) {
        B.push_back(make_pair(A[i], false));
      }
      if (A[i] < 0) {
        B.push_back(make_pair(-A[i], true));
      }
    }
    N -= zero;
    sort(B.begin(), B.end());
    reverse(B.begin(), B.end());
    long long prod1 = 1;
    int cnt = 0;
    for (int i = 0; i < K; i++) {
      prod1 *= B[i].first;
      prod1 %= MOD;
      if (B[i].second) {
        cnt++;
      }
    }
    if (cnt % 2 == 0) {
      cout << prod1 << endl;
    } else {
      if (N == K) {
        if (zero > 0) {
          cout << 0 << endl;
        } else {
          cout << (MOD - prod1) % MOD << endl;
        }
      } else {
        bool pos1 = false;
        long long pos_small;
        bool neg1 = false;
        long long neg_small;
        for (int i = 0; i < K; i++) {
          if (B[i].second) {
            neg1 = true;
            neg_small = B[i].first;
          } else {
            pos1 = true;
            pos_small = B[i].first;
          }
        }
        bool pos2 = false;
        long long pos_large;
        bool neg2 = false;
        long long neg_large;
        for (int i = K; i < N; i++) {
          if (B[i].second) {
            if (!neg2) {
              neg_large = B[i].first;
            }
            neg2 = true;
          } else {
            if (!pos2) {
              pos_large = B[i].first;
            }
            pos2 = true;
          }
        }
        if ((pos1 && neg2) && !(neg1 && pos2)) {
          prod1 *= modinv(pos_small);
          prod1 %= MOD;
          prod1 *= neg_large;
          prod1 %= MOD;
          cout << prod1 << endl;
        } else if (!(pos1 && neg2) && (neg1 && pos2)) {
          prod1 *= modinv(neg_small);
          prod1 %= MOD;
          prod1 *= pos_large;
          prod1 %= MOD;
          cout << prod1 << endl;
        } else if (!(pos1 && neg2) && !(neg1 && pos2)) {
          if (zero > 0) {
            cout << 0 << endl;
          } else {
            long long prod2 = 1;
            for (int i = N - K; i < N; i++) {
              prod2 *= B[i].first;
              prod2 %= MOD;
            }
            cout << (MOD - prod2) % MOD << endl;
          }
        } else {
          if (pos_large * pos_small < neg_large * neg_small) {
            prod1 *= modinv(pos_small);
            prod1 %= MOD;
            prod1 *= neg_large;
            prod1 %= MOD;
            cout << prod1 << endl;
          } else {
            prod1 *= modinv(neg_small);
            prod1 %= MOD;
            prod1 *= pos_large;
            prod1 %= MOD;
            cout << prod1 << endl;
          }
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long modpow(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
long long modinv(long long a) { return modpow(a, MOD - 2); }
int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int zero = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0) {
      zero++;
    }
  }
  if (zero > N - K) {
    cout << 0 << endl;
  } else {
    vector<pair<long long, bool>> B;
    for (int i = 0; i < N; i++) {
      if (A[i] > 0) {
        B.push_back(make_pair(A[i], false));
      }
      if (A[i] < 0) {
        B.push_back(make_pair(-A[i], true));
      }
    }
    N -= zero;
    sort(B.begin(), B.end());
    reverse(B.begin(), B.end());
    long long prod1 = 1;
    int cnt = 0;
    for (int i = 0; i < K; i++) {
      prod1 *= B[i].first;
      prod1 %= MOD;
      if (B[i].second) {
        cnt++;
      }
    }
    if (cnt % 2 == 0) {
      cout << prod1 << endl;
    } else {
      if (N == K) {
        if (zero > 0) {
          cout << 0 << endl;
        } else {
          cout << (MOD - prod1) % MOD << endl;
        }
      } else {
        bool pos1 = false;
        int pos_small;
        bool neg1 = false;
        int neg_small;
        for (int i = 0; i < K; i++) {
          if (B[i].second) {
            neg1 = true;
            neg_small = B[i].first;
          } else {
            pos1 = true;
            pos_small = B[i].first;
          }
        }
        bool pos2 = false;
        int pos_large;
        bool neg2 = false;
        int neg_large;
        for (int i = K; i < N; i++) {
          if (B[i].second) {
            if (!neg2) {
              neg_large = B[i].first;
            }
            neg2 = true;
          } else {
            if (!pos2) {
              pos_large = B[i].first;
            }
            pos2 = true;
          }
        }
        /*
        cout << pos_small << endl;
        cout << neg_small << endl;
        cout << pos_large << endl;
        cout << neg_large << endl;
        */
        if ((pos1 && neg2) && !(neg1 && pos2)) {
          prod1 *= modinv(pos_small);
          prod1 %= MOD;
          prod1 *= neg_large;
          prod1 %= MOD;
          cout << prod1 << endl;
        } else if (!(pos1 && neg2) && (neg1 && pos2)) {
          prod1 *= modinv(neg_small);
          prod1 %= MOD;
          prod1 *= pos_large;
          prod1 %= MOD;
          cout << prod1 << endl;
        } else if (!(pos1 && neg2) && !(neg1 && pos2)) {
          if (zero > 0) {
            cout << 0 << endl;
          } else {
            long long prod2 = 1;
            for (int i = N - K; i < N; i++) {
              prod2 *= B[i].first;
              prod2 %= MOD;
            }
            cout << (MOD - prod2) % MOD << endl;
          }
        } else {
          assert(pos1 && neg1 && pos2 && neg2);
          if (pos_large * pos_small < neg_large * neg_small) {
            prod1 *= modinv(pos_small);
            prod1 %= MOD;
            prod1 *= neg_large;
            prod1 %= MOD;
            cout << prod1 << endl;
          } else {
            assert(false);
            prod1 *= modinv(neg_small);
            prod1 %= MOD;
            prod1 *= pos_large;
            prod1 %= MOD;
            cout << prod1 << endl;
          }
        }
      }
    }
  }
}