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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
const double PI = 3.141592653589793238463;
using namespace std;
using ll = long long;
const ll INF = +10010010000;
typedef pair<ll, ll> P;
const int mod = 1e9 + 7;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x -= mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t % 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i < n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] + ifact[n - k];
  }
};

// int f(unsigned long a){
//     int t =0;
//     while(a/10>0){
//         a/=10;
//         ++t;
//     }
//     return t;
// }

int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  long long ans = 0;
  // bool flag = 0 ;
  // bool zero = 0;
  // unsigned long a;
  // cin >> ans;
  ans = A[0];
  rep(i, n) {
    if (A[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  rrep(i, n - 1) {
    if (A[i] > (unsigned long)pow(10, 18) / ans) {
      // flag=1;
      cout << -1 << endl;
      return 0;
    }
    ans *= A[i];
    // if(a==0)zero=1;
    // keta_ans = f(ans);
    // cout << keta_ans <<": "<< keta<<endl;
  }
  // if(zero)cout<<0<<endl;
  // if(flag)cout << -1<<endl;
  cout << ans << endl;
  // if(ans<=(unsigned long)pow(10,18))cout<<ans<<endl;
  // else cout << -1 <<endl;
  // unsigned long tmp =1000000000000000001;
  // if(tmp>pow(10,18))cout << "yes"<<endl;
  // else cout << "no"<<endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
const double PI = 3.141592653589793238463;
using namespace std;
using ll = long long;
const ll INF = +10010010000;
typedef pair<ll, ll> P;
const int mod = 1e9 + 7;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x -= mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t % 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i < n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] + ifact[n - k];
  }
};

// int f(unsigned long a){
//     int t =0;
//     while(a/10>0){
//         a/=10;
//         ++t;
//     }
//     return t;
// }

int main() {
  int n;
  cin >> n;
  unsigned long ans = 0;
  bool flag = 0;
  bool zero = 0;
  unsigned long a;
  cin >> ans;
  rrep(i, n - 1) {
    cin >> a;
    // keta  = f(a);
    // if(keta+keta_ans>18)flag=1;
    // if(i==0)ans = a;
    if (a > (unsigned long)pow(10, 18) / ans) {
      flag = 1;
      // cout << pow(10,18)/ans<<endl;
    }
    ans *= a;
    if (a == 0)
      zero = 1;
    // keta_ans = f(ans);
    // cout << keta_ans <<": "<< keta<<endl;
  }
  if (zero)
    cout << 0 << endl;
  else if (flag)
    cout << -1 << endl;
  else
    cout << ans << endl;
  // if(ans<=(unsigned long)pow(10,18))cout<<ans<<endl;
  // else cout << -1 <<endl;
  // unsigned long tmp =1000000000000000001;
  // if(tmp>pow(10,18))cout << "yes"<<endl;
  // else cout << "no"<<endl;
  return 0;
}