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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define S(x) scanf("%d", &x)
#define SS(x, y) scanf("%d%d", &x, &y)
#define all(c) (c).begin(), (c).end()
#define sz(c) int((c).size())
#define pb push_back
#define present(c, x) ((c).find(x) != (c).end())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define REV(i, x, y) for (int i = x; i >= y; i--)
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define FILL(a, v) memset(a, v, sizeof(a))
#define DB(x) cerr << #x << " = " << x << endl
#define debugarr(A, a, b)                                                      \\
  cerr << #A << " : ";                                                         \\
  rep(i, a, b) cerr << A[i] << " ";                                            \\
  cerr << endl;
// #define R(x) 			((rand()%x) + 1)

typedef long long int LL;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

/* ------------------------Main Code----------------------- */
const int MAX = 200001, mod = 1e9 + 7;

LL A[MAX];

bool cmp(const LL &x, const LL &y) { return abs(x) < abs(y); }

LL findMin(int l, int r, int parity) {
  LL mn = mod;
  rep(i, l, r) if (A[i] * parity > 0) mn = min(mn, abs(A[i]));
  return mn;
}

LL findMax(int l, int r, int parity) {
  LL mx = -1;
  rep(i, l, r) if (A[i] * parity > 0) mx = max(mx, abs(A[i]));
  return mx;
}

LL inv(LL a, LL b, LL m) {
  if (b == 0)
    return 1;
  LL k = inv(a, (b >> 1), m);
  k = (k * k) % m;
  if (b & 1)
    k = (k * a) % m;
  return k;
}

int main() {

  int n, k;
  SS(n, k);
  rep(i, 0, n - 1) scanf("%lld", &A[i]);
  sort(A, A + n, cmp);
  int l = 0, r = n - 1, cntNegative = 0;
  LL res = 1;
  rep(i, 1, k) {
    res = (res * abs(A[n - i])) % mod;
    if (A[n - i] < 0)
      cntNegative++;
  }
  if (cntNegative & 1) {
    LL abovePositive = findMin(n - k, n - 1, 1);
    LL belowNegative = findMax(0, n - k - 1, -1);
    LL aboveNegative = findMin(n - k, n - 1, -1);
    LL belowPositive = findMax(0, n - k - 1, 1);
    if (abovePositive != mod && aboveNegative != mod && belowPositive != -1 &&
        belowNegative != -1) {
      if (belowNegative * aboveNegative < belowPositive * abovePositive) {
        res = (res * belowPositive) % mod;
        res = (res * inv(aboveNegative, mod - 2, mod)) % mod;
      } else {
        res = (res * belowNegative) % mod;
        res = (res * inv(abovePositive, mod - 2, mod)) % mod;
      }
    } else if (abovePositive != mod && belowNegative != -1) {
      res = (res * belowNegative) % mod;
      res = (res * inv(abovePositive, mod - 2, mod)) % mod;
    } else if (aboveNegative != mod && belowPositive != -1) {
      res = (res * belowPositive) % mod;
      res = (res * inv(aboveNegative, mod - 2, mod)) % mod;
    } else {

      rep(i, 0, n - 1) if (A[i] < 0) { A[i] *= -1; }
      sort(A, A + n);
      res = 1;
      rep(i, 0, k - 1) res = (res * A[i]) % mod;
      if (res != 0)
        res = mod - res;
    }
  }
  printf("%lld\\n", res);

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define S(x) scanf("%d", &x)
#define SS(x, y) scanf("%d%d", &x, &y)
#define all(c) (c).begin(), (c).end()
#define sz(c) int((c).size())
#define pb push_back
#define present(c, x) ((c).find(x) != (c).end())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define REV(i, x, y) for (int i = x; i >= y; i--)
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define FILL(a, v) memset(a, v, sizeof(a))
#define DB(x) cerr << #x << " = " << x << endl
#define debugarr(A, a, b)                                                      \\
  cerr << #A << " : ";                                                         \\
  rep(i, a, b) cerr << A[i] << " ";                                            \\
  cerr << endl;
// #define R(x) 			((rand()%x) + 1)

typedef long long int LL;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

/* ------------------------Main Code----------------------- */
const int MAX = 200001, mod = 1e9 + 7;

LL A[MAX];

bool cmp(const LL &x, const LL &y) { return abs(x) < abs(y); }

LL findMin(int l, int r, int parity) {
  LL mn = mod;
  rep(i, l, r) if (A[i] * parity > 0) mn = min(mn, abs(A[i]));
  return mn;
}

LL findMax(int l, int r, int parity) {
  LL mx = -1;
  rep(i, l, r) if (A[i] * parity > 0) mx = max(mx, abs(A[i]));
  return mx;
}

LL inv(LL a, LL b, LL m) {
  if (b == 0)
    return 1;
  LL k = inv(a, (b >> 1), m);
  k = (k * k) % m;
  if (b & 1)
    k = (k * a) % m;
  return k;
}

int main() {
  int t;
  S(t);
  while (t--) {
    int n, k;
    SS(n, k);
    rep(i, 0, n - 1) scanf("%lld", &A[i]);
    sort(A, A + n, cmp);
    int l = 0, r = n - 1, cntNegative = 0;
    LL res = 1;
    rep(i, 1, k) {
      res = (res * abs(A[n - i])) % mod;
      if (A[n - i] < 0)
        cntNegative++;
    }
    if (cntNegative & 1) {
      LL abovePositive = findMin(n - k, n - 1, 1);
      LL belowNegative = findMax(0, n - k - 1, -1);
      LL aboveNegative = findMin(n - k, n - 1, -1);
      LL belowPositive = findMax(0, n - k - 1, 1);
      if (abovePositive != mod && aboveNegative != mod && belowPositive != -1 &&
          belowNegative != -1) {
        if (belowNegative * aboveNegative < belowPositive * abovePositive) {
          res = (res * belowPositive) % mod;
          res = (res * inv(aboveNegative, mod - 2, mod)) % mod;
        } else {
          res = (res * belowNegative) % mod;
          res = (res * inv(abovePositive, mod - 2, mod)) % mod;
        }
      } else if (abovePositive != mod && belowNegative != -1) {
        res = (res * belowNegative) % mod;
        res = (res * inv(abovePositive, mod - 2, mod)) % mod;
      } else if (aboveNegative != mod && belowPositive != -1) {
        res = (res * belowPositive) % mod;
        res = (res * inv(aboveNegative, mod - 2, mod)) % mod;
      } else {

        rep(i, 0, n - 1) if (A[i] < 0) { A[i] *= -1; }
        sort(A, A + n);
        res = 1;
        rep(i, 0, k - 1) res = (res * A[i]) % mod;
        if (res != 0)
          res = mod - res;
      }
    }
    printf("%lld\\n", res);
  }
  return 0;
}