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

#include <stdio.h>
long long a[100010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  int zero = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == 0)
      zero++;
  if (zero > 0) {
    // a[i] contains 0
    printf("0\\n");
    return 0;
  }
  long long prod = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] <= 1000000000000000000 / prod) {
      // This condition is equivalent to prod*a[i] <=10ˆ18
      prod *= a[i];
    } else {
      printf("-1\\n");
      return 0;
    }
  }
  printf("%lld\\n", prod);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <list>
#define ll long long int
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define rep(a, b, c) for (a = b; a < c; a++)
#define repj(a, b, c) for (a = b; a < c; a++)
#define repr for (i = n - 1; i >= 0; i--)
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define inf 100000001
const double pie = 3.141592653589793238463;
#define mod 1000000007
#define lb lower_bound
#define up upper_bound
#define rep1 for (i = 1; i <= n; i++)
#define vr vector
#define POP(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define ef else if
#define all(x) (x).begin(), (x).end()
#define SORT(A) sort(A.begin(), A.end());
#define REV(A) reverse(A.begin(), A.end());
#define lenth length()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
const ll dx[4] = {-1, 1, 0, 0};
const ll dy[4] = {0, 0, -1, 1};
ll XX[] = {-1, -1, -1, 0, 0, 1, 1, 1};
ll YY[] = {-1, 0, 1, -1, 1, -1, 0, 1};
#define precise(x) cout << fixed << setprecision(x)
#define t1(x) cerr << #x << ": " << x << endl
#define t2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define t3(x, y, z)                                                            \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define t4(a, b, c, d)                                                         \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define t5(a, b, c, d, e)                                                      \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define t6(a, b, c, d, e, f)                                                   \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl
using namespace std;
void SieveOfEratosthenes(ll n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // for (ll p=2; p<=n; p++)
  // if (prime[p])
  // cout << p << " ";
}

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int escape(vector<vector<char>> v, ll i, ll j, ll n, ll m) {
  if (i >= n || j >= m || i < 0 || j < 0)
    return 0;
  if (v[i][j] != '.')
    return 0;
  if (i == n - 1 && j == m - 1)
    return 1;
  escape(v, i + 1, j, n, m);
  escape(v, i, j + 1, n, m);
  escape(v, i - 1, j, n, m);
  escape(v, i, j - 1, n, m);

  return 1;
}
string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

int main() {
  // fastio
  ll t, i, j, k, l, m, n, a, x, b, y, z, c, d, e, p, nl, np, q, r;
  string str2, str3;
  ll arr[10010];
  cin >> n;
  z = 0;
  rep(i, 0, n) {
    cin >> arr[i];
    if (arr[i] == 0)
      z++;
  }
  if (z > 0) {
    cout << 0 << endl;
    return 0;
  }
  ll mul = 1;
  rep(i, 0, n) {
    if (arr[i] <= 1000000000000000000 / mul) {
      mul *= arr[i];
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << mul << endl;
}