#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000000 #define Inf64 1000000000000000001LL int main(){ int n; cin>>n; vector a(n); vector> pos(n); rep(i,n){ cin>>a[i]; a[i]--; pos[a[i]].push_back(i); } if(pos[n-1].size()==0){ cout<<0< ngs(n,0); rep(i,n-1){ rep(j,pos[i].size()){ rep(k,pos[n-1].size()){ int x = pos[i][j] - pos[n-1][k]; if(x<0)x+=n; ngs[x]++; } } } mint ans = n; int used = 0; ngs[0] = 100; for(int i=n-2;i>=0;i--){ vector t(n); rep(j,pos[i].size()){ rep(k,pos[n-1].size()){ int x = pos[i][j] - pos[n-1][k]; if(x<0)x+=n; ngs[x]--; t[x]++; } } int C = 0; rep(j,n){ if(t[j]==pos[i].size() && ngs[j]==0)C++; } if(pos[i].size()>0)ans *= C; else ans *= C - (n-2-i); } cout<