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

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

//  a^b % m
long long powmod(long long a, long long b, long long m) {
  long long r = 1;
  for (; b > 0; b >>= 1, a = a * a % m)
    if (b & 1)
      r = r * a % m;
  return r;
}

long long solve(int N, int K, vector<long long> A) {
  sort(A.begin(), A.end(),
       [&](long long a, long long b) { return abs(a) > abs(b); });

  long long ans = 1;
  long long M = 1000000007LL;

  bool allneg = true;
  for (int a : A)
    if (a >= 0)
      allneg = false;
  if (allneg && K % 2 != 0) {
    for (int i = 0; i < K; i++)
      ans = (ans * A[N - i - 1] % M + M) % M;
  } else {
    int sign = 1;
    for (int i = 0; i < K; i++) {
      ans = (ans * A[i] % M + M) % M;
      sign *= (A[i] > 0 ? 1 : A[i] < 0 ? -1 : 0);
    }

    if (sign < 0) {
      long long t00 = -1;
      for (int i = K - 1; i >= 0 && t00 == -1; i--)
        if (A[i] > 0)
          t00 = i;
      long long t01 = -1;
      for (int i = K; i < N && t01 == -1; i++)
        if (A[i] < 0)
          t01 = i;
      long long t10 = -1;
      for (int i = K - 1; i >= 0 && t10 == -1; i--)
        if (A[i] < 0)
          t10 = i;
      long long t11 = -1;
      for (int i = K; i < N && t11 == -1; i++)
        if (A[i] > 0)
          t11 = i;

      if (t00 != -1 && t01 != -1 && t10 != -1 && t11 != -1 &&
              A[t01] * A[t10] > A[t11] * A[t00] ||
          t00 != -1 && t01 != -1 && !(t10 != -1 && t11 != -1)) {
        ans = ans * powmod((A[t00] % M + M) % M, M - 2, M) % M;
        ans = (ans * A[t01] % M + M) % M;
      } else if (t10 != -1 && t11 != -1) {
        ans = ans * powmod((A[t10] % M + M) % M, M - 2, M) % M;
        ans = (ans * A[t11] % M + M) % M;
      } else {
        for (long long a : A)
          if (a == 0)
            ans = 0;
      }
    }
  }
  return ans;
}

long long naive(int N, int K, vector<long long> A) {
  long long ans = -99999999;
  for (int b = 0; b < 1 << N; b++) {
    int c = 0;
    for (int i = 0; i < N; i++)
      c += b >> i & 1;
    if (c == K) {
      long long a = 1;
      for (int i = 0; i < N; i++)
        if (b >> i & 1)
          a *= A[i];
      if (a > ans)
        ans = a;
    }
  }
  return (ans % 1000000007 + 1000000007) % 1000000007;
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A(N);
  for (long long &a : A)
    cin >> a;
  cout << solve(N, K, A) << endl;

  // while (true)
  //{
  //     int N = rand()%8+1;
  //     int K = rand()%N+1;
  //     vector<long long> A(N);
  //     for (long long &a: A)
  //         a = rand()%17-8;

  //    if (solve(N, K, A)!=naive(N, K, A))
  //    {
  //        cout<<solve(N, K, A)<<"!="<<naive(N, K, A)<<endl;
  //        cout<<N<<" "<<K<<endl;
  //        for (long long a: A)
  //            cout<<a<<endl;
  //        break;
  //    }
  //}
}


Error: 0

Fixed submission:

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

//  a^b % m
long long powmod(long long a, long long b, long long m) {
  long long r = 1;
  for (; b > 0; b >>= 1, a = a * a % m)
    if (b & 1)
      r = r * a % m;
  return r;
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A(N);
  for (long long &a : A)
    cin >> a;

  sort(A.begin(), A.end(),
       [&](long long a, long long b) { return abs(a) > abs(b); });

  long long ans = 1;
  long long M = 1000000007LL;

  bool allneg = true;
  for (int a : A)
    if (a >= 0)
      allneg = false;
  if (allneg && K % 2 != 0) {
    for (int i = 0; i < K; i++)
      ans = (ans * A[N - i - 1] % M + M) % M;
  } else {
    int sign = 1;
    for (int i = 0; i < K; i++) {
      ans = (ans * A[i] % M + M) % M;
      sign *= (A[i] > 0 ? 1 : A[i] < 0 ? -1 : 0);
    }

    if (sign < 0) {
      long long t00 = -1;
      for (int i = K - 1; i >= 0 && t00 == -1; i--)
        if (A[i] > 0)
          t00 = i;
      long long t01 = -1;
      for (int i = K; i < N && t01 == -1; i++)
        if (A[i] < 0)
          t01 = i;
      long long t10 = -1;
      for (int i = K - 1; i >= 0 && t10 == -1; i--)
        if (A[i] < 0)
          t10 = i;
      long long t11 = -1;
      for (int i = K; i < N && t11 == -1; i++)
        if (A[i] > 0)
          t11 = i;

      if (t00 != -1 && t01 != -1 && t10 != -1 && t11 != -1 &&
              abs(A[t00] * A[t01]) > abs(A[t10] * A[t11]) ||
          t00 != -1 && t01 != -1 && !(t10 != -1 && t11 != -1)) {
        ans = ans * powmod((A[t00] % M + M) % M, M - 2, M) % M;
        ans = (ans * A[t01] % M + M) % M;
      } else if (t10 != -1 && t11 != -1) {
        return -1;
        ans = ans * powmod((A[t10] % M + M) % M, M - 2, M) % M;
        ans = (ans * A[t11] % M + M) % M;
      } else {
        for (long long a : A)
          if (a == 0)
            ans = 0;
      }
    }
  }
  cout << ans << endl;
}