#include<iostream>

int cnt[100005];
const long long mod = 998244353;
int max_val = 0;
long long ans = 1;

long long mpow(int a, int b) {
	if (!a) return 0LL;
	if (!b) return 1LL;
	return b%2 ? (long long)a * mpow(a, b-1) % mod : mpow((long long)a*a%mod, b/2);
}

int main(void) {
	int n;
	std::cin >> n;
	for (int i = 0; i < n; ++i) {
		int d;
		std::cin >> d;
		if (d > max_val) max_val = d;
		if (!i && d || i && !d) {
			std::cout << 0 << '\n';
			return 0;
		}
		cnt[d]++;
	}

	for (int i = 2; i <= max_val; i++) {
		ans = (ans * mpow(cnt[i-1], cnt[i])) % mod;
	}

	std::cout << ans << '\n';
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(long long i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define rrep(i,n) for(long long i=int(n);i>0;i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+5  
using ll = long long; 
using P=pair<int,int>;
using lP=pair<ll,ll>;
using fP=pair<double,double>;
using PPI=pair<P,int>;
//ll const mod=998244353;
ll const mod=1e9+7;
const ll MAX=1000005;
using vi=vector<int>;
using vl=vector<ll>;
using vc=vector<char>;
using vd=vector<double>;
using vs=vector<string>;
using vp=vector<P>;
using vb=vector<bool>;
using vvi =vector<vector<int>>;
using vvl =vector<vector<ll>>;
using vvd=vector<vector<double>>;
using vvc=vector<vector<char>>;
using vvp =vector<vector<P>>;
using vvb=vector<vector<bool>>;
template <typename T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}


int main(){
    int n;
    cin>>n;
    vi a(n);
    rep(i,n)cin>>a[i];
    reverse(all(a));
    ll ans=a[0];
    bool ok=true;
    arep(i,1,n){
        if(a[i]==a[i-1]-1)continue;
        else if(a[i]<a[i-1]-1)ok=false;
        ans+=a[i];
    }
    if(a[n-1]!=0)ok=false;
    if(ok)cout<<ans<<endl;
    else cout<<-1<<endl;
    return 0;
}