#include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int P = 998244353; ll powmod(ll n, ll k) { ll r = 1, t = n % P; for (; k != 0; k /= 2) { if (k & 1) r = r * t % P; t = t * t % P; } return r; } ll modinv(ll n) { return powmod(n, P - 2); } vector f0, f1; void init(int n) { f0.resize(n + 1); f0[0] = 1; for (int i = 1; i <= n; i++) { f0[i] = (ll)f0[i - 1] * i % P; } f1.resize(n + 1); f1[n] = modinv(f0[n]); for (int i = n; i > 0; i--) { f1[i - 1] = (ll)f1[i] * i % P; } } ll fact(int k) { return f0[k]; } ll comb(int n, int k) { if (n < k || k < 0) return 0; return (ll)f0[n] * f1[k] % P * f1[n - k] % P; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector p(n); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } vector q(n, 0), u; for (int i = 0; i < n; i++) { if (q[i]) continue; int k = 1; q[i] = 1; for (int j = p[i]; j != i; j = p[j]) { k++; q[j] = 1; } u.push_back(k); } init(n); vector y(m + 1); for (int i = 1; i <= m; i++) { y[i] = powmod(i, u.size()); } auto f = [&](int n) { if (n < m) return 0LL; ll r = 0; for (int i = 1; i <= m; i++) { r += y[i] * comb(m, i) % P * ((m - i) % 2 == 0 ? 1 : -1); } r %= P; if (r < 0) r += P; r = r * f1[m] % P; return r; }; vector dp(n, 0); dp[0] = 1; for (int k : u) { for (int i = n; --i >= 0;) { ll t = 0; for (int j = 0; j < k && j <= i; j++) { t += (ll)dp[i - j] * (j == k - 1 ? 1 : comb(k, j)) % P; } dp[i] = t % P; } } ll r = 0; for (int i = (int)u.size(); i <= n; i++) { r += f(i) * dp[n - i] % P * ((n - i) % 2 == 0 ? 1 : -1); for (int i = 1; i <= m; i++) { y[i] = (ll)y[i] * i % P; } } r %= P; if (r < 0) r += P; cout << r << endl; return 0; }