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

/// Bismillahir Rahmanir Rahim
// Author: Tanvir Hussain
// ICE,NSTU
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
const long long MOD = 1000000007;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define SET(x) memset(x, 0, sizeof(x))
#define SET2d(x, m, n) memset(x, 0, sizeof(x[0][0]) * m * n)
#define SETBOOL(x) memset(x, false, sizeof(x))
#define CLR(x) memset(x, -1, sizeof(x))
#define CLR2d(x, m, n) memset(x, -1, sizeof(x[0][0]) * m * n)
#define mp make_pair
#define PII pair<int, int>
#define pf printf
#define sf scanf
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define np std::string::npos
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(), v.end())
#define maxv(v) *max_element(v.begin(), v.end())
#define cases(cs, t) for (int cs = 1; cs <= t; cs++)
#define PI acos(-1)
#define no1 __builtin_popcount
#define BOUNDARY(i, j) ((i >= 0 && i < row) && (j >= 0 && j < column))
#define uniq(vec)                                                              \\
  vec.resize(distance(vec.begin(), unique(vec.begin(), vec.end())))
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define sz(a) int(a.size())
#define ff first
#define ss second
#define endl "\\n"
#define forch(it, s) for (auto it : s)
#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define bits(n) __builtin_popcount(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const int maxx = 100005;

inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); } /// When MOD is prime.
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

// this fuction sorts vector pair according to first element in descending
// order.
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}

template <typename T> inline T Bigmod(T base, T power, T MOD) {
  T ret = 1;
  while (power) {
    if (power & 1)
      ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
double sq(double x) { return x * x; }
ll po(ll b, ll p) {
  ll res = 1;
  while (p) {
    res *= b;
    p--;
  }
  return res;
}
ll lg2(ll x) {
  ll res = 0;
  while (x > 1) {
    res++;
    x /= 2ll;
  }
  return res;
}
void solve() {
  int n, k;
  cin >> n >> k;
  deque<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(ALL(a));
  if (a[n - 1] == 0 and k & 1) {
    cout << 0 << endl;
    return;
  }
  if (a[n - 1] <= 0 and k & 1) {
    ll ans = 1;
    reverse(ALL(a));
    rep(i, k) { ans = modMul(ans, a[i]); }
    cout << ans << endl;
    return;
  }
  int i = 0, j = n - 1;
  ll ans = 1;
  if (k & 1) {
    ans = modMul(ans, a[j]);
    j--;
    k--;
  }
  k >>= 1;
  rep(it, k) {
    ll left = a[i] * a[i + 1];
    ll right = a[j] * a[j - 1];
    if (left > right) {
      ans = modMul(ans, left);
      i += 2;
    } else {
      ans = modMul(ans, right);
      j -= 2;
    }
  }
  if (ans < 0) {
    ans += MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}

signed main()

{
  IOS;
  /*#ifndef ONLINE_JUDGE
      freopen ("input.txt","r",stdin);
      freopen ("output.txt","w",stdout);
  #endif*/
  int t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}
/// Alhamdulillah


Error: 0

Fixed submission:

/// Bismillahir Rahmanir Rahim
// Author: Tanvir Hussain
// ICE,NSTU
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
const long long MOD = 1000000007;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define SET(x) memset(x, 0, sizeof(x))
#define SET2d(x, m, n) memset(x, 0, sizeof(x[0][0]) * m * n)
#define SETBOOL(x) memset(x, false, sizeof(x))
#define CLR(x) memset(x, -1, sizeof(x))
#define CLR2d(x, m, n) memset(x, -1, sizeof(x[0][0]) * m * n)
#define mp make_pair
#define PII pair<int, int>
#define pf printf
#define sf scanf
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define np std::string::npos
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(), v.end())
#define maxv(v) *max_element(v.begin(), v.end())
#define cases(cs, t) for (int cs = 1; cs <= t; cs++)
#define PI acos(-1)
#define no1 __builtin_popcount
#define BOUNDARY(i, j) ((i >= 0 && i < row) && (j >= 0 && j < column))
#define uniq(vec)                                                              \\
  vec.resize(distance(vec.begin(), unique(vec.begin(), vec.end())))
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define sz(a) int(a.size())
#define ff first
#define ss second
#define endl "\\n"
#define forch(it, s) for (auto it : s)
#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define bits(n) __builtin_popcount(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const int maxx = 100005;

inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); } /// When MOD is prime.
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

// this fuction sorts vector pair according to first element in descending
// order.
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}

template <typename T> inline T Bigmod(T base, T power, T MOD) {
  T ret = 1;
  while (power) {
    if (power & 1)
      ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
double sq(double x) { return x * x; }
ll po(ll b, ll p) {
  ll res = 1;
  while (p) {
    res *= b;
    p--;
  }
  return res;
}
ll lg2(ll x) {
  ll res = 0;
  while (x > 1) {
    res++;
    x /= 2ll;
  }
  return res;
}
void solve() {
  int n, k;
  cin >> n >> k;
  deque<ll> a(n), neg, pos;
  rep(i, n) {
    cin >> a[i];
    if (a[i] <= 0)
      neg.pb(a[i]);
    else
      pos.pb(a[i]);
  }
  /*if(pos.size()==0){
      sort(ALL(neg));
      if(k%2==0){
              ll ans=1;
          rep(i,k){
              ll val=abs(neg[i]);
              ans=modMul(ans,val);
          }
          normal(ans);
          cout<<ans<<endl;
      }
      else{
          reverse(ALL(neg));
          ll ans=1;
          rep(i,k-1){
              ll val=abs(neg[i]);
              ans=modMul(ans,val);
          }
          ans=(ans%MOD*(neg[k-1]+MOD)%MOD)%MOD;
          cout<<ans<<endl;
      }
      return;
  }
  if(neg.size()==0){
      sort(ALL(pos));
      ll ans=1;
      reverse(ALL(pos));
      rep(i,k){
          ans=modMul(ans,pos[i]);
      }
      cout<<ans<<endl;
      return;
  }
  */
  vector<pair<ll, pair<ll, ll>>> vec;
  sort(ALL(a));
  if (pos.size() == 0) {
    sort(ALL(neg));
    if (k % 2 == 0) {
      ll ans = 1;
      rep(i, k) { ans = modMul(ans, neg[i]); }
      cout << ans << endl;
    } else {
      reverse(ALL(neg));
      ll ans = 1;
      rep(i, k) {
        ll val = abs(neg[i]);
        ans = modMul(ans, val);
      }
      cout << ans << endl;
    }
    return;
  }
  if (neg.size() == 0) {
    sort(ALL(pos));
    ll ans = 1;
    reverse(ALL(pos));
    rep(i, k) { ans = modMul(ans, pos[i]); }
    cout << ans << endl;
    return;
  }
  rep(i, n) {
    vec.pb({a[i] * a[i + 1], {a[i], a[i + 1]}});
    i++;
  }
  if (n & 1) {
    vec.pb({a[n - 1], {a[n - 1], highest(ll)}});
  }
  sort(ALL(vec));
  reverse(ALL(vec));
  ll ans = 1;
  rep(i, k) {
    ans = modMul(ans, vec[i].first);
    i++;
  }
  if (k & 1) {
    ans = modMul(ans, vec[k - 1].second.first);
  }
  cout << ans << endl;
}

signed main()

{
  IOS;
  /*#ifndef ONLINE_JUDGE
      freopen ("input.txt","r",stdin);
      freopen ("output.txt","w",stdout);
  #endif*/
  int t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}
/// Alhamdulillah