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

#include <bits/stdc++.h>

using namespace std;

bool cmp(int a, int b) { return abs(a) > abs(b); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long int M = 1000000007;

  int n, k;
  cin >> n >> k;
  long int a[n];
  long long ans = 1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n, cmp);
  for (int i = 0; i < k; i++) {
    ans = (ans * a[i]) % M;
  }
  if (ans >= 0) {
    cout << (ans + M) % M << "\\n";
  } else {
    int x = -1, y = -1, z = -1, w = -1;
    for (int i = k - 1; i >= 0; i--) {
      if (a[i] > 0 && x == -1)
        x = i;
      if (a[i] < 0 && y == -1)
        y = i;
      if (x != -1 && y != -1)
        break;
    }
    for (int i = k; i < n; i++) {
      if (a[i] > 0 && z == -1)
        z = i;
      if (a[i] < 0 && w == -1)
        w = i;
      if (z != -1 && w != -1)
        break;
    }
    ans = 1;
    if ((x == -1 || w == -1) && (y == -1 || z == -1)) {
      for (int i = n - 1; i >= n - k; i--) {
        ans = (ans * a[i]) % M;
      }
    } else if (x == -1 || w == -1) {
      for (int i = 0; i < k; i++) {
        if (i == y)
          ans = (ans * a[z]) % M;
        else
          ans = (ans * a[i]) % M;
      }
    } else if (y == -1 || z == -1) {
      for (int i = 0; i < k; i++) {
        if (i == x)
          ans = (ans * a[w]) % M;
        else
          ans = (ans * a[i]) % M;
      }
    } else {
      if (a[x] * a[z] > a[y] * a[w]) {
        for (int i = 0; i < k; i++) {
          if (i == y)
            ans = (ans * a[z]) % M;
          else
            ans = (ans * a[i]) % M;
        }
      } else {
        for (int i = 0; i < k; i++) {
          if (i == x)
            ans = (ans * a[w]) % M;
          else
            ans = (ans * a[i]) % M;
        }
      }
    }
    cout << (ans + M) % M << "\\n";
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define li long int
#define ll long long
#define pi pair<int, int>
#define pli pair<long int, long int>
#define vi vector<int>
#define vli vector<long int>
#define vll vector<long long>
#define vpi vector<pair<int, int>>
#define vpli vector<pair<long int, long int>>
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define REP(i, init, n) for (li i = init; i < n; i++)
#define INF 0x3f3f3f3f

template <class T> T NEGMOD(T x) { return (x % MOD + MOD) % MOD; }

inline li solve(li *a, li n, li k) {
  sort(a, a + n);
  li ans = 1;
  if (k % 2 == 1) {
    if (a[n - 1] == 0)
      return 0;

    if (a[n - 1] < 0) {
      for (li i = n - 1; i >= n - k; i--)
        ans = NEGMOD(NEGMOD(ans) * NEGMOD(a[i]));

      return ans;
    }

    if (a[n - 1] > 0) {
      ans *= a[n - 1];
      k--;
      n--;
    }
  }

  if (k % 2 == 0) {
    li fp = 0, lp = n - 1;
    while (k > 0) {
      if (a[fp] * a[fp + 1] < a[lp] * a[lp - 1]) {
        ans = NEGMOD(NEGMOD(ans) * NEGMOD(NEGMOD(a[lp]) * NEGMOD(a[lp - 1])));
        lp -= 2;
      } else {
        ans = NEGMOD(NEGMOD(ans) * NEGMOD(NEGMOD(a[fp]) * NEGMOD(a[fp + 1])));
        fp += 2;
      }
      k -= 2;
    }

    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  li t;
  cin >> t;
  while (t--) {
    li n, k;
    cin >> n >> k;
    li a[n];
    REP(i, 0, n) cin >> a[i];

    cout << solve(a, n, k) << "\\n";
  }
}