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

#include <bits/stdc++.h>
using namespace std;
long MOD = 1e9 + 7, n, k, ans = 1, l, r, flg = 1, a[1 << 18], x, y;
int main() {
  cin >> n >> k;
  r = n - 1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  if (k % 2)
    ans = a[r], r--, k--, flg = (ans < 0 ? -1 : 1);
  while (k) {
    x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * flg > y * flg)
      ans *= x % MOD, ans %= MOD, l += 2;
    else
      ans *= y % MOD, ans %= MOD, r -= 2;
    k -= 2;
  }
  cout << (ans % MOD + MOD) % MOD << endl;
}


Error: 0

Fixed submission:

#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// #include<ratio>
#include <algorithm>
#include <numeric>
// #include<limits>
// #include<chrono>
// #include<random>
// #include<regex>
// #include<functional>
#include <boost/multiprecision/cpp_int.hpp>
// <iostream>
using std::cerr;
using std::cin;
using std::clog;
using std::cout;
using std::endl;
// <ios>
using std::ios_base;
// <string>
using std::stoi;
using std::stol;
using std::string;
using std::to_string;
// <vector>
using std::vector;
// <map>
using std::map;
// <set>
using std::set;
// <queue>
using std::priority_queue;
using std::queue;
// <stack>
using std::stack;
// <deque>
using std::deque;
// <utility>
using std::pair;
// <tuple>
using std::tuple;
// <bitset>
using std::bitset;
// <complex>
using std::abs;
using std::acos;
using std::acosh;
using std::asin;
using std::asinh;
using std::atan;
using std::atanh;
using std::complex;
using std::cos;
using std::cosh;
using std::exp;
using std::imag;
using std::log;
using std::log10;
using std::norm;
using std::polar;
using std::pow;
using std::real;
using std::sin;
using std::sinh;
using std::sqrt;
using std::tan;
using std::tanh;
// <ratio>
// using std::ratio;
// <algorithm>
using std::all_of;
using std::any_of;
using std::binary_search;
using std::count;
using std::equal_range;
using std::fill;
using std::find;
using std::lower_bound;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::minmax;         // value
using std::minmax_element; // pointer
using std::next_permutation;
using std::none_of;
using std::nth_element;
using std::partial_sort;
using std::prev_permutation;
using std::reverse;
using std::rotate;
using std::set_difference;
using std::set_intersection;
using std::set_union;
using std::shuffle;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;
using std::upper_bound;
// <numeric>
using std::accumulate;
// <limits>
// using std::numeric_limits;
// <chrono>
// using std::chrono::system_clock;
// <random>
// using std::mt19937_64;
// <regex>
// using std::regex_match;
// <functional>
// using std::function;
// <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
// using vi=vector<int>;
// using vll=vector<ll>;
// using vs=vector<string>;
// using msi=map<string,int>;
// using si=set<int>;
// using ss=set<string>;
// using pii=pair<int,int>;
// using tiii=tuple<int,int,int>;
// using pqi=priority_queue<int>;
// using pqpii=priority_queue<pii>;
// using pqtiii=priority_queue<tiii>;
#define BEGIN                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define END return EXIT_SUCCESS
#define up(I, A, B) for (auto I = (A); I <= (B); ++I)
#define dw(I, A, B) for (auto I = (A); I >= (B); --I)
#define rep(I, N) up(I, 0, N - 1)
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define ft first
#define sd second
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define rs resize
#define np next_permutation
#define pp prev_permutation
#define rev reverse
template <class T> inline void in(T &p) { cin >> p; }
template <class T1, class T2> inline void in(T1 &p1, T2 &p2) {
  cin >> p1 >> p2;
}
template <class T1, class T2, class T3> inline void in(T1 &p1, T2 &p2, T3 &p3) {
  cin >> p1 >> p2 >> p3;
}
template <class T1, class T2, class T3, class T4>
inline void in(T1 &p1, T2 &p2, T3 &p3, T4 &p4) {
  cin >> p1 >> p2 >> p3 >> p4;
}
template <class T1, class T2, class T3, class T4, class T5>
inline void in(T1 &p1, T2 &p2, T3 &p3, T4 &p4, T5 &p5) {
  cin >> p1 >> p2 >> p3 >> p4 >> p5;
}
template <class T> inline void ins(T &p) {
  for_each(all(p), in<decltype(p[0])>);
}
template <class T> inline void out(T p) { cout << p << "\\n"; }
template <class T1, class T2> inline void out(T1 p1, T2 p2) {
  cout << p1 << " " << p2 << "\\n";
}
template <class T1, class T2, class T3> inline void out(T1 p1, T2 p2, T3 p3) {
  cout << p1 << " " << p2 << " " << p3 << "\\n";
}
template <class T1, class T2, class T3, class T4>
inline void out(T1 p1, T2 p2, T3 p3, T4 p4) {
  cout << p1 << " " << p2 << " " << p3 << " " << p4 << "\\n";
}
inline void out(ld p) { printf("%.12Lf\\n", p); }
template <class T> inline void outml(T p) {
  for_each(all(p), out<decltype(p[0])>);
} // ml=multi line
template <class T> inline void outol(T V) {
  auto n = V.size();
  rep(i, n) cout << V[i] << " \\n"[i == n - 1];
} // ol=one line
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
cpp_int N, K, ans = -1, mod = (long)1e9 + 7, tmp = 1;
inline void solve(void) {
  in(N, K);
  vector<cpp_int> A((long)N);
  ins(A);
  sort(rall(A));
  rep(i, K) tmp *= A[(long)i];
  chmax(ans, tmp);
  up(i, K, N - 1) {
    tmp /= A[(long)(i - K)];
    tmp *= A[(long)i];
    chmax(ans, tmp);
  }
  ans += mod;
  ans %= mod;
  out(ans);
}
int main(void) {
  BEGIN;
  solve();
  END;
}