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

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(vector<vector<T>> x) {
  cerr << "\\n{";
  int f = x.size();
  for (int i = 0; i < f; i++) {
    cerr << "(";
    int g = x[i].size();
    for (auto j : x[i]) {
      cerr << j;
      (--g != 0) ? cerr << "," : cerr << "";
    }
    cerr << ")\\n";
  }
  cerr << "}";
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void __print(vector<bool> x) {
  cerr << "{";
  int f = x.size();
  for (auto i : x) {
    cerr << i;
    (--f != 0) ? cerr << "," : cerr << "";
  }
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = [", _print(x)
#else
#define trace(x...)
#endif

template <typename... T> void read(T &...args) { ((cin >> args), ...); }

template <typename... T> void writ(T &&...args) {
  ((cout << args << " "), ...);
}
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define INF (ll)1e18
#define IINF (int)1e9
#define EPS 1e-9
/*
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    In the blazing sun I saw you
    In the shadows hiding from yourself
    When the lights are on I know you
    See you're grey from all the lies you tell
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  bool flag = false;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    ans = (ans * temp) % MOD;
    if (temp >= 0) {
      pos.push_back(temp);
      flag = true;
    } else
      neg.push_back(temp);
  }
  if (k == n)
    cout << ((ans % MOD) + MOD) % MOD;
  else if (!flag) {
    ans = 1;
    sort(neg.begin(), neg.end());
    if (k & 1) {
      for (int i = 0; i < k; i++)
        ans = (ans * neg[n - i - 1]) % MOD;
    } else {
      for (int i = 0; i < k; i++)
        ans = (ans * neg[i]) % MOD;
    }
    cout << ((ans % MOD) + MOD) % MOD;
  } else {
    sort(pos.begin(), pos.end(), greater<ll>());
    sort(neg.begin(), neg.end());
    vector<ll> prepos(pos.size() + 1, 1);
    vector<ll> preneg(neg.size() + 1, 1);
    for (int i = 1; i <= pos.size(); i++)
      prepos[i] = (prepos[i - 1] * pos[i - 1]) % MOD;
    for (int i = 1; i <= neg.size(); i++)
      preneg[i] = (preneg[i - 1] * neg[i - 1]) % MOD;
    // trace(prepos,preneg);
    ll posl = pos.size();
    ll ip = min(posl, k);
    ll in = k - ip;
    if (in & 1) {
      ip--;
      in++;
    }
    // trace(ip,in);
    while (in + 2 <= neg.size() && ip >= 2 &&
           pos[ip - 1] * pos[ip - 2] < neg[in + 1] * neg[in]) {
      ip -= 2;
      in += 2;
    }
    // trace(ip,in);
    cout << (prepos[ip] * preneg[in]) % MOD << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(vector<vector<T>> x) {
  cerr << "\\n{";
  int f = x.size();
  for (int i = 0; i < f; i++) {
    cerr << "(";
    int g = x[i].size();
    for (auto j : x[i]) {
      cerr << j;
      (--g != 0) ? cerr << "," : cerr << "";
    }
    cerr << ")\\n";
  }
  cerr << "}";
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void __print(vector<bool> x) {
  cerr << "{";
  int f = x.size();
  for (auto i : x) {
    cerr << i;
    (--f != 0) ? cerr << "," : cerr << "";
  }
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = [", _print(x)
#else
#define trace(x...)
#endif

template <typename... T> void read(T &...args) { ((cin >> args), ...); }

template <typename... T> void writ(T &&...args) {
  ((cout << args << " "), ...);
}
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define INF (ll)1e18
#define IINF (int)1e9
#define EPS 1e-9
/*
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    In the blazing sun I saw you
    In the shadows hiding from yourself
    When the lights are on I know you
    See you're grey from all the lies you tell
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  bool flag = false;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    ans = (ans * temp) % MOD;
    if (temp > 0) {
      pos.push_back(temp);
      flag = true;
    } else
      neg.push_back(temp);
  }
  if (k == n)
    cout << ((ans % MOD) + MOD) % MOD;
  else if (!flag) {
    ans = 1;
    for (int i = 0; i < n; i++) {
      if (k)
        ans = (ans * neg[i]) % MOD;
      else
        break;
      k--;
    }
    cout << ((ans % MOD) + MOD) % MOD;
  } else {
    ll ans_temp = 1;
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end(), greater<ll>());
    if (k & 1) {
      k--;
      ans_temp *= pos[0];
      pos.front() = move(pos.back());
      pos.pop_back();
    }
    for (int i = 0, j = 0; i < pos.size() || j < neg.size();) {
      if (k <= 0)
        break;
      if (j + 1 < neg.size() && i + 1 < pos.size() &&
          neg[j] * neg[j + 1] > pos[i] * pos[i + 1]) {
        ans_temp = (ans_temp * neg[j] * neg[j + 1]) % MOD;
        j += 2;
        trace(ans_temp, j);
      } else {
        ans_temp = (ans_temp * pos[i] * pos[i + 1]) % MOD;
        i += 2;
        trace(ans_temp, i);
      }
      k -= 2;
    }
    cout << ((ans_temp % MOD) + MOD) % MOD;
  }
  return 0;
}