#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const int mod = 998244353;
int a[600000];
int f[300001];

int main() {
    int n, p = -1, c, num = 0, i, j, k;
    long long ans = 1;
    vector <pair<int, pair<int, int>>> v;
    set <pair<int, int>> s;
    set <pair<int, int>>::iterator it, it2;
    
    scanf("%d", &n);
    
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    
    for (i = n; i < n * 2; i++) a[i] = a[i - n];
    
    if (a[0] == 1) {
        p = n;
        
        for (i = n - 1; i >= 0; i--) {
            if (a[i] != 1) break;
            p--;
        }
    } else {
        for (i = 0; i < n; i++) {
            if (a[i] == 1) {
                p = i;
                break;
            }
        }
    }
    
    if (p == -1) {
        puts("0");
        return 0;
    } else if (p == 0) {
        for (i = 2; i <= n; i++) ans = ans * i % mod;
        printf("%lld\n", ans);
        return 0;
    }
    
    for (i = p; i < p + n; ) {
        if (f[a[i]] == 1) {
            puts("0");
            return 0;
        }
        
        f[a[i]] = 1;
        
        for (j = i; j < p + n; j++) {
            if (a[i] != a[j]) break;
        }
        
        if (a[i] == 1) {
            c = j - i;
        } else if (j - i > c) {
            puts("0");
            return 0;
        }
        
        v.push_back(make_pair(a[i], make_pair(i, j - i)));
        i = j;
    }
    
    sort(v.begin(), v.end());
    
    s.insert(make_pair(-1e9, -1e9));
    s.insert(make_pair(1e9, 1e9));
    
    for (i = 1, j = 0; i <= n; i++) {
        if (j == v.size() || v[j].first != i) {
            ans = ans * num % mod;
            num--;
        } else {
            int x = v[j].first;
            int y = v[j].second.first;
            int z = v[j].second.second;
            
            j++;
            
            if (a[y + z] > x) {
                if (a[y - 1] > x && z != c) {
                    puts("0");
                    return 0;
                }
            } else if (a[y - 1] < x) {
                ans = ans * (c - z + 1) % mod;
            }
            
            it = s.lower_bound(make_pair(y, 0));
            it2 = it;
            it2--;
            
            if (it2->second == y && it->first == y + z) {
                int l = it2->first;
                int r = it->second;
                
                num += z - 1 + c - 1;
                s.erase(it);
                s.erase(it2);
                s.insert(make_pair(l, r));
            } else if (it2->second == y) {
                int l = it2->first;
                int r = y + z;
                
                if (y + z == p + n) num += c - 1;
                
                num += z - 1;
                s.erase(it2);
                s.insert(make_pair(l, r));
            } else if (it->first == y + z) {
                int l = y;
                int r = it->second;
                
                num += z - 1;
                s.erase(it);
                s.insert(make_pair(l, r));
            } else {
                if (y + z == p + n) num += z - 1;
                
                s.insert(make_pair(y, y + z));
            }
        }
    }
    
    printf("%lld\n", ans);
    
    return 0;
}
