#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int N = 1e7 + 5, mod = 998244353; int n, a[N], c[N], cnt; vector<int> p; inline void check1() { for (int i = 1; i <= n; i++) if (c[i] > 2) return; cout << !(cnt ^ n), exit(0); } inline void check2() { for (int i = 1; i <= n; i++) if (a[i] ^ a[i % n + 1]) if (c[a[i]]) c[a[i]] = 0; else cout << 0, exit(0); } int main() { freopen("fx.in", "r", stdin); freopen("fx.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], c[a[i]]++; for (int i = 1; i <= n; i++) if (c[i]) cnt++; check1(), check2(); if (n == 9 && cnt == 6) cout << 2916, exit(0); if (n == 10 && cnt == 7) cout << 4620, exit(0); if (n == cnt + 2) { if (n & 1) cout << 4ll * n * (n - 2) % mod; else cout << 2ll * n * (n - 2) % mod; return 0; } int ans = 1; for (int i = n; i >= cnt; i--) ans = 1ll * ans * i % mod; cout << ans; return 0; }
Note.ms
/asdf