Problem p02866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(X) << Y << endl
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N;
int A[100010];
int num[100010];

int mod_pow(int b, int e, int m) {
  if (e == 0) {
    return 1;
  } else {
    int sum = mod_pow(b, e / 2, m);
    if (e % 2 == 0) {
      return (sum * sum) % m;
    } else {
      return (((sum * sum) % m) * b) % m;
    }
  }
}

signed main() {
  cin >> N;
  int cnt = 0;
  REP(i, N) {
    cin >> A[i];
    if (A[i] == 0) {
      cnt++;
    }
  }
  if (A[0] != 0 || cnt > 1) {
    cout << 0 << endl;
    return 0;
  }
  REP(i, N) { num[A[i]] = true; }
  for (int i = 0; i <= MAX(A, A + N); i++) {
    if (num[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  int s = 1;
  int m = 998244353;
  map<int, int> H;
  REP(i, N) { H[A[i]]++; }
  int t = 0;
  cnt = 0;
  for (auto x : H) {
    if (cnt >= 1) {
      s *= mod_pow(t, x.second, m);
      s %= m;
    }
    t = x.second;
    cnt++;
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(X) << Y << endl
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N;
int A[100010];
int num[100010];

int mod_pow(int b, int e, int m) {
  if (e == 0) {
    return 1;
  } else {
    int sum = mod_pow(b, e / 2, m);
    if (e % 2 == 0) {
      return (sum * sum) % m;
    } else {
      return (((sum * sum) % m) * b) % m;
    }
  }
}

signed main() {
  cin >> N;
  int cnt = 0;
  REP(i, N) {
    cin >> A[i];
    if (A[i] == 0) {
      cnt++;
    }
  }
  if (A[0] != 0 || cnt > 1) {
    cout << 0 << endl;
    return 0;
  }
  REP(i, N) { num[A[i]] = true; }
  int M = MAX(A, A + N);
  for (int i = 0; i <= M; i++) {
    if (num[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  int s = 1;
  int m = 998244353;
  map<int, int> H;
  REP(i, N) { H[A[i]]++; }
  int t = 0;
  cnt = 0;
  for (auto x : H) {
    if (cnt >= 1) {
      s *= mod_pow(t, x.second, m);
      s %= m;
    }
    t = x.second;
    cnt++;
  }
  cout << s << endl;
  return 0;
}