#include using namespace std; long long mod = 998244353; //入力が必ず-mod>= 1; } return ret; }; mint &operator=(const mint &b) = default; mint operator-() const {return mint(0)-(*this);} mint operator+(const mint b){return mint(v)+=b;} mint operator-(const mint b){return mint(v)-=b;} mint operator*(const mint b){return mint(v)*=b;} mint operator/(const mint b){return mint(v)/=b;} mint operator+=(const mint b){ v += b.v; if(v >= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b){return v == b.v;} bool operator!=(const mint b){return v != b.v;} bool operator>(const mint b){return v > b.v;} bool operator>=(const mint b){return v >= b.v;} bool operator<(const mint b){return v < b.v;} bool operator<=(const mint b){return v <= b.v;} mint pow(const long long x){return repeat2mint(v,x);} mint inv(){return mint(1)/v;} }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector A(N); for(auto &a : A) cin >> a,a--; vector start; for(int i=0; i>> B; for(auto s : start){ vector now = {N-1}; for(int i=(s+1)%N; ; i=(i+1)%N){ if(A.at(i) == N-1) break; now.push_back(A.at(i)); } B.emplace_back(pair{s,now}); } sort(B.begin(),B.end(),[&](auto &a,auto &b){return a.second.size() now1 = B.at(i).second; for(int k=i-1; k>=0; k--){ vector now2 = B.at(k).second; while(now1.size() > now2.size()) now1.pop_back(); if(now1 != now2){cout << "0\n"; return 0;} } } */ { auto [p,ign] = B.back(); rotate(A.begin(),A.begin()+p,A.end()); for(auto &[s,ign] : B) s = (s-p+N)%N; start.clear(); for(int i=0; i> check(N); for(auto s : start) for(int i=0; i> sepa(N); for(int i=0; i already(N); for(int i=0; i count(N); for(auto g : sepa.at(i)) for(auto c : check.at(g)) count.at(c)++; int kouho = 0; vector all; for(int k=0; k 0 && !already.at(k)) all.push_back(k); } answer *= kouho; hold--; for(auto &p : all) answer *= hold,hold--,already.at(p) = true; if(hold < 0){cout << "0\n"; return 0;} } while(hold > 0) answer *= hold,hold--; answer *= N; cout << answer.v << "\n"; return 0; vector AA(N); iota(AA.begin(),AA.end(),0); int answer2 = 0; do{ if(AA.at(0) != N-1) continue; vector now(N); for(int i=0; i