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

#include <bits/stdc++.h>
using namespace std;

#define debi(var)                                                              \\
  "[" << (#var) << " : " << var << "]"                                         \\
      << " "
#define debv(v)                                                                \\
  "[" << (#v) << " : {";                                                       \\
  for (int i = 0; i < v.size() - 1; ++i)                                       \\
    cout << v[i] << ", ";                                                      \\
  cout << v.back() << "}] "
#define deba(a, n)                                                             \\
  "[" << (#a) << " : {";                                                       \\
  for (int i = 0; i < n - 1; ++i)                                              \\
    cout << a[i] << ", ";                                                      \\
  cout << a[n - 1] << "}] "
#define debp(p) "[" << (#p) << " : {" << p.first << ", " << p.second << "}] "
#define debs(s)                                                                \\
  "[" << (#s) << " : {";                                                       \\
  auto _itr = s.begin();                                                       \\
  for (int k = s.size(); k > 1; --k) {                                         \\
    cout << *_itr << ", ";                                                     \\
    ++_itr;                                                                    \\
  }                                                                            \\
  cout << *_itr << "}] "

typedef long long ll;
/*LETS BEGIN WITH THE NAME OF LORD MAHADEV*/
int main() {
  int n;
  cin >> n;
  bool zero = false, greater = false;
  ll ans = 1;
  while (n--) {
    ll val;
    cin >> val;
    if (!val) {
      zero = true;
      break;
    }
    if (val <= (ll)1e18 / ans) {
      ans *= val;
    } else {
      greater = true;
    }
  }
  if (zero) {
    cout << 0 << endl;
  } else if (greater) {
    cout << -1 << endl;
  } else {
    cout << ans;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define debi(var)                                                              \\
  "[" << (#var) << " : " << var << "]"                                         \\
      << " "
#define debv(v)                                                                \\
  "[" << (#v) << " : {";                                                       \\
  for (int i = 0; i < v.size() - 1; ++i)                                       \\
    cout << v[i] << ", ";                                                      \\
  cout << v.back() << "}] "
#define deba(a, n)                                                             \\
  "[" << (#a) << " : {";                                                       \\
  for (int i = 0; i < n - 1; ++i)                                              \\
    cout << a[i] << ", ";                                                      \\
  cout << a[n - 1] << "}] "
#define debp(p) "[" << (#p) << " : {" << p.first << ", " << p.second << "}] "
#define debs(s)                                                                \\
  "[" << (#s) << " : {";                                                       \\
  auto _itr = s.begin();                                                       \\
  for (int k = s.size(); k > 1; --k) {                                         \\
    cout << *_itr << ", ";                                                     \\
    ++_itr;                                                                    \\
  }                                                                            \\
  cout << *_itr << "}] "

typedef long long ll;
/*LETS BEGIN WITH THE NAME OF LORD MAHADEV*/
int main() {
  int n;
  cin >> n;
  bool zero = false, greater = false;
  ll ans = 1;
  while (n--) {
    ll val;
    cin >> val;
    if (!val) {
      zero = true;
    }
    if (val <= (ll)1e18 / ans) {
      ans *= val;
    } else {
      greater = true;
    }
  }
  if (zero) {
    cout << 0 << endl;
  } else if (greater) {
    cout << -1 << endl;
  } else {
    cout << ans;
  }
  return 0;
}