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

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

// DEBUG TEMPLATE
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x < 0)
      neg.pb(x);
    if (x > 0)
      pos.pb(x);
    if (x == 0)
      zero++;
  }
  if (n - k < zero) {
    cout << 0 << endl;
    return;
  }
  sort(all(neg));
  sort(rall(pos));
  // debug() << imie(neg) imie(pos);
  int ans = 1;
  int posind = 0, negind = 0;
  int negsz = neg.size(), possz = pos.size();
  debug() << imie(negsz) imie(possz) imie(zero);
  if (negsz == n) {
    if (k % 2 == 0) {
      for (int i = 0; i < n && k; i++) {
        ans = (ans * neg[i] % MOD);
        k--;
      }
    } else {
      for (int i = n - 1; i > -1 && k; i--) {
        ans = (ans * neg[i]) % MOD;
        k--;
      }
    }
  }
  if (possz == n) {
    for (int i = 0; i < n && k; i++) {
      ans = (ans * pos[i]) % MOD;
      k--;
    }
  }

  for (int used = 0; used < k;) {
    if (used == k - 1) {
      if (posind < possz) {
        ans = (ans * pos[posind++]) % MOD;
      } else {
        if (k == 1) {
          ans = (ans * neg[negsz - 1]) % MOD;
          if (ans < 0)
            ans += MOD;
        } else {
          // debug() << imie(negind);
          if (negind + 1 < negsz) {
            ans = (ans * power(neg[0], MOD - 2)) % MOD;
            ans = (ans * neg[negsz - 2]) % MOD;
            ans = (ans * neg[negsz - 1]) % MOD;
          } else {
            ans = (ans * neg[negind++]) % MOD;
          }
        }
      }
      used++;
    } else {
      int curpos = 0;
      int curneg = 0;
      if (posind + 1 < possz) {
        curpos = 1;
        curpos *= pos[posind++];
        curpos *= pos[posind++];
      }
      if (negind + 1 < negsz) {
        curneg = 1;
        curneg *= neg[negind++];
        curneg *= neg[negind++];
      }
      if (curpos == 0 && curneg == 0) {
        curpos = 1;
        curpos *= pos[posind++];
        curpos *= neg[negind++];
        curpos %= MOD;
        ans = (ans * curpos) % MOD;
      } else {
        if (curpos > curneg) {
          curpos %= MOD;
          ans = (ans * curpos) % MOD;
          if (curneg != 0)
            negind -= 2;
        } else {
          curneg %= MOD;
          ans = (ans * curneg) % MOD;
          if (curpos != 0)
            posind -= 2;
        }
      }
      used += 2;
    }
    // debug() << imie(negind) imie(posind);
    // debug() << imie(ans);
  }
  if (ans < 0 && zero)
    ans = 0;
  ans = ((ans % MOD) + MOD) % MOD;
  cout << ans << endl;
}

/*
  5 4 3 2 1

  5

  4 5 -> 4
  4 5 3 -> 7





*/
signed main() {
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

// DEBUG TEMPLATE
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x < 0)
      neg.pb(x);
    if (x > 0)
      pos.pb(x);
    if (x == 0)
      zero++;
  }
  if (n - k < zero) {
    cout << 0 << endl;
    return;
  }
  sort(all(neg));
  sort(rall(pos));
  debug() << imie(neg) imie(pos);
  int ans = 1;
  int posind = 0, negind = 0;
  int negsz = neg.size(), possz = pos.size();

  if (negsz == n) {
    if (k % 2 == 0) {
      for (int i = 0; i < n && k; i--) {
        ans = (ans * neg[i] % MOD);
        k--;
      }
    } else {
      for (int i = n - 1; i > -1 && k; i--) {
        ans = (ans * neg[i]) % MOD;
        k--;
      }
    }
  }

  for (int used = 0; used < k;) {
    if (used == k - 1) {
      if (posind < possz) {
        ans = (ans * pos[posind++]) % MOD;
      } else {
        if (k == 1) {
          ans = (ans * neg[negsz - 1]) % MOD;
          if (ans < 0)
            ans += MOD;
        } else {
          debug() << imie(negind);
          if (negind + 1 < negsz) {
            ans = (ans * power(neg[0], MOD - 2)) % MOD;
            ans = (ans * neg[negsz - 2]) % MOD;
            ans = (ans * neg[negsz - 1]) % MOD;
          } else {
            ans = (ans * neg[negind++]) % MOD;
          }
        }
      }
      used++;
    } else {
      int curpos = 0;
      int curneg = 0;
      if (posind + 1 < possz) {
        curpos = 1;
        curpos *= pos[posind++];
        curpos *= pos[posind++];
      }
      if (negind + 1 < negsz) {
        curneg = 1;
        curneg *= neg[negind++];
        curneg *= neg[negind++];
      }
      if (curpos == 0 && curneg == 0) {
        curpos = 1;
        curpos *= pos[posind++];
        curpos *= neg[negind++];
        curpos %= MOD;
        ans = (ans * curpos) % MOD;
      } else {
        if (curpos > curneg) {
          curpos %= MOD;
          ans = (ans * curpos) % MOD;
          if (curneg != 0)
            negind -= 2;
        } else {
          curneg %= MOD;
          ans = (ans * curneg) % MOD;
          if (curpos != 0)
            posind -= 2;
        }
      }
      used += 2;
    }
    // debug() << imie(negind) imie(posind);
    debug() << imie(ans);
  }
  ans = ((ans % MOD) + MOD) % MOD;
  cout << ans << endl;
}

/*
  5 4 3 2 1

  5

  4 5 -> 4
  4 5 3 -> 7





*/
signed main() {
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(