Problem p02658 (C++)

Buggy submission (status: Runtime Error):
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
#define dcml(n) fixed << setprecision(n)
// CONST
//------------------------------------------
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;
const ld EPS = 1e-12;
const ld PI = 3.14159265358979323846;
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define FORS(i, m, n) for (ll(i) = (m); (i) <= (n); (i)++)
#define RFOR(i, m, n) for (ll(i) = (n)-1; (i) >= (n); (i)--)
#define RFORS(i, m, n) for (ll(i) = (n); (i) > (n); (i)--)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
#define WREPS(i, in, j, jn) REPS(i, in) REPS(j, jn)
//-----------------------------------------
// namespace
using namespace std;
// ライブラリはここに
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//-----------------------------------------
int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll ans = 1;
  bool flag = false;
  REP(i, N) {
    if (ans <= 1000000000000000000LL / A[i]) {
      ans *= A[i];
    } else {
      flag = 1;
    }
  }
  if (!flag) {
    cout << ans << "\n";
  } else {
    if (ans == 0)
      cout << 0 << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}


Fixed submission:
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
#define dcml(n) fixed << setprecision(n)
// CONST
//------------------------------------------
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;
const ld EPS = 1e-12;
const ld PI = 3.14159265358979323846;
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define FORS(i, m, n) for (ll(i) = (m); (i) <= (n); (i)++)
#define RFOR(i, m, n) for (ll(i) = (n)-1; (i) >= (n); (i)--)
#define RFORS(i, m, n) for (ll(i) = (n); (i) > (n); (i)--)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
#define WREPS(i, in, j, jn) REPS(i, in) REPS(j, jn)
//-----------------------------------------
// namespace
using namespace std;
// ライブラリはここに
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//-----------------------------------------
int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll ans = 1;
  bool flag = false;
  REP(i, N) {
    if (A[i] == 0) {
      cout << 0 << "\n";
      return 0;
    }
    if (ans <= 1000000000000000000LL / A[i]) {
      ans *= A[i];
    } else {
      flag = 1;
    }
  }
  if (!flag) {
    cout << ans << "\n";
  } else {
    if (ans == 0)
      cout << 0 << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}