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

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937
    rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b) { return (a % b); }
inline ll poww(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  while (b) {
    a %= b, swap(a, b);
  }
  return a;
}
void read(vector<int> &w, int n) {
  w.resize(n);
  for (int i = 0; i < n; i++)
    cin >> w[i];
}
void print(vector<int> &w) {
  for (int i = 0; i < sz(w); i++) {
    if (i == sz(w) - 1)
      cout << w[i] << "\\n";
    else
      cout << w[i] << " ";
  }
}
int prodmod(vector<int> w);
int summod(vector<int> w);
/// CUIDADO COM MAXN
#define N 300050

int n, m, q, k, v[N], ans;
pii w[N];
string s;
int getS(int x) {
  if (x == 0)
    return 0;
  if (x > 0)
    return 1;
  return -1;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  vector<pii> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.pb({abs(x), x});
  }
  sort(all(v));
  reverse(all(v));
  int curr = 1, sign = 1;
  for (int i = 0; i < k; i++) {
    curr *= v[i].f;
    curr %= mod;
    if (curr < 0)
      curr %= mod;
    int S = getS(v[i].s); // v[i].s/v[i].f;
    if (S < 0)
      sign *= -1;
    if (!S) {
      cout << "0\\n";
      return 0;
    }
  }
  if (sign == 1)
    cout << curr << "\\n";
  else {
    int can0 = 0;
    for (int i = 0; i < n; i++) {
      if (v[i].f == 0)
        can0 = 1;
    }
    // tiro 1 neg e boto 1 pos
    // tiro 1 pos e boto 1 neg
    int opt1, opt2;
    bool can1 = 0, can2 = 0;
    for (int i = k; i < n; i++) {
      if (v[i].s > 0 and !can1) {
        can1 = 1;
        opt1 = v[i].f;
      }
      if (v[i].s < 0 and !can2) {
        can2 = 1;
        opt2 = v[i].f;
      }
    }
    int cara1, cara2;
    if (can1) {
      int can = 0;
      for (int i = 0; i < k; i++) {
        if (v[i].s < 0) {
          can = 1;
          cara1 = v[i].f;
        }
      }
      can1 = can;
    }

    if (can2) {
      int can = 0;
      for (int i = 0; i < k; i++) {
        if (v[i].s > 0) {
          can = 1;
          cara2 = v[i].f;
        }
      }
      can2 = can;
    }
    // cout<<can1<<" "<<can2<<"\\n";
    if (can1 and can2) {
      // cout<<"1 "<<opt1<<" "<<cara1<<"\\n";
      // cout<<"2 "<<opt2<<" "<<cara2<<"\\n";
      int L = opt1 * cara2, R = opt2 * cara1;
      if (L > R) {
        int ans = (curr * opt1) % mod;
        ans = (ans * poww(cara1, mod - 2)) % mod;
        cout << ans << "\\n";
        return 0;
      } else {
        int ans = (curr * opt2) % mod;
        ans = (ans * poww(cara2, mod - 2)) % mod;
        cout << ans << "\\n";
        return 0;
      }
    }
    if (can1) {
      int ans = (curr * opt1) % mod;
      ans = (ans * poww(cara1, mod - 2)) % mod;
      cout << ans << "\\n";
      return 0;
    }
    if (can2) {
      int ans = (curr * opt2) % mod;
      ans = (ans * poww(cara2, mod - 2)) % mod;
      cout << ans << "\\n";
      return 0;
    }
    if (can0)
      cout << "0\\n";
    else {
      // cout<<"ola\\n";
      int prod = 1;
      vector<int> aux;
      for (auto x : v)
        aux.pb(x.s);
      sort(all(aux));
      reverse(all(aux));
      for (int i = 0; i < k; i++) {
        // cout<<"x "<<aux[i]<<"\\n";
        prod = (prod * aux[i]) % mod;
        if (prod < 0)
          prod += mod;
      }
      cout << prod << "\\n";
      return 0;
    }
  }
}
/*
    CUIDADO COM MAXN, MOD, OVERFLOW
    >Colocar (>DEFINE INT LONG LONG<) no inicio do template
    >mod = (1e9 + 7), por padrao (NAO ESQUECER DE ALTERAR)
    >NAO ESQUECER DEFINE INT LONG LONG
    > N = 1 ?
*/
int summod(vector<int> w) {
  int curr = 0;
  for (int i = 0; i < sz(w); i++) {
    curr = (curr + w[i]) % mod;
    if (curr < 0)
      curr += mod;
  }
  return curr;
}
int prodmod(vector<int> w) {
  int curr = 1;
  for (int i = 0; i < sz(w); i++) {
    if (w[i] >= mod)
      w[i] %= mod;
    curr = (curr * w[i]) % mod;
    if (curr < 0)
      curr += mod;
  }
  return curr;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937
    rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b) { return (a % b); }
inline ll poww(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  while (b) {
    a %= b, swap(a, b);
  }
  return a;
}
void read(vector<int> &w, int n) {
  w.resize(n);
  for (int i = 0; i < n; i++)
    cin >> w[i];
}
void print(vector<int> &w) {
  for (int i = 0; i < sz(w); i++) {
    if (i == sz(w) - 1)
      cout << w[i] << "\\n";
    else
      cout << w[i] << " ";
  }
}
int prodmod(vector<int> w);
int summod(vector<int> w);
/// CUIDADO COM MAXN
#define N 300050

int n, m, q, k, v[N], ans;
pii w[N];
string s;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  vector<pii> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.pb({abs(x), x});
  }
  sort(all(v));
  reverse(all(v));
  int curr = 1, sign = 1;
  for (int i = 0; i < k; i++) {
    curr *= v[i].f;
    curr %= mod;
    if (curr < 0)
      curr %= mod;
    int S = v[i].s / v[i].f;
    if (S < 0)
      sign *= -1;
  }
  if (sign == 1)
    cout << curr << "\\n";
  else {
    int can0 = 0;
    for (int i = 0; i < n; i++) {
      if (v[i].f == 0)
        can0 = 1;
    }
    // tiro 1 neg e boto 1 pos
    // tiro 1 pos e boto 1 neg
    int opt1, opt2;
    bool can1 = 0, can2 = 0;
    for (int i = k; i < n; i++) {
      if (v[i].s > 0 and !can1) {
        can1 = 1;
        opt1 = v[i].f;
      }
      if (v[i].s < 0 and !can2) {
        can2 = 1;
        opt2 = v[i].f;
      }
    }
    int cara1, cara2;
    if (can1) {
      int can = 0;
      for (int i = 0; i < k; i++) {
        if (v[i].s < 0) {
          can = 1;
          cara1 = v[i].f;
        }
      }
      can1 = can;
    }

    if (can2) {
      int can = 0;
      for (int i = 0; i < k; i++) {
        if (v[i].s > 0) {
          can = 1;
          cara2 = v[i].f;
        }
      }
      can2 = can;
    }
    // cout<<can1<<" "<<can2<<"\\n";
    if (can1 and can2) {
      // cout<<"1 "<<opt1<<" "<<cara1<<"\\n";
      // cout<<"2 "<<opt2<<" "<<cara2<<"\\n";
      int L = opt1 * cara2, R = opt2 * cara1;
      if (L > R) {
        int ans = (curr * opt1) % mod;
        ans = (ans * poww(cara1, mod - 2)) % mod;
        cout << ans << "\\n";
        return 0;
      } else {
        int ans = (curr * opt2) % mod;
        ans = (ans * poww(cara2, mod - 2)) % mod;
        cout << ans << "\\n";
        return 0;
      }
    }
    if (can1) {
      int ans = (curr * opt1) % mod;
      ans = (ans * poww(cara1, mod - 2)) % mod;
      cout << ans << "\\n";
      return 0;
    }
    if (can2) {
      int ans = (curr * opt2) % mod;
      ans = (ans * poww(cara2, mod - 2)) % mod;
      cout << ans << "\\n";
      return 0;
    }
    if (can0)
      cout << "0\\n";
    else {
      // cout<<"ola\\n";
      int prod = 1;
      vector<int> aux;
      for (auto x : v)
        aux.pb(x.s);
      sort(all(aux));
      reverse(all(aux));
      for (int i = 0; i < k; i++) {
        // cout<<"x "<<aux[i]<<"\\n";
        prod = (prod * aux[i]) % mod;
        if (prod < 0)
          prod += mod;
      }
      cout << prod << "\\n";
      return 0;
    }
  }
}
/*
    CUIDADO COM MAXN, MOD, OVERFLOW
    >Colocar (>DEFINE INT LONG LONG<) no inicio do template
    >mod = (1e9 + 7), por padrao (NAO ESQUECER DE ALTERAR)
    >NAO ESQUECER DEFINE INT LONG LONG
    > N = 1 ?
*/
int summod(vector<int> w) {
  int curr = 0;
  for (int i = 0; i < sz(w); i++) {
    curr = (curr + w[i]) % mod;
    if (curr < 0)
      curr += mod;
  }
  return curr;
}
int prodmod(vector<int> w) {
  int curr = 1;
  for (int i = 0; i < sz(w); i++) {
    if (w[i] >= mod)
      w[i] %= mod;
    curr = (curr * w[i]) % mod;
    if (curr < 0)
      curr += mod;
  }
  return curr;
}