#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(),(x).end() #define outl(x) cout << x << endl #define SP << " " << #define inf 1e18 #define mod 998244353 using namespace std; typedef long long llint; typedef long long ll; typedef pair P; ll n, m; ll p[5005]; ll dp[5005][5005]; ll dp2[5005][5005][2]; bool used[5005]; vector vec; const int FACT_MAX = 200005; llint fact[FACT_MAX], fact_inv[FACT_MAX]; llint modpow(llint a, llint n) { if(n == 0) return 1; if(n % 2){ return ((a%mod) * (modpow(a, n-1)%mod)) % mod; } else{ return modpow((a*a)%mod, n/2) % mod; } } void make_fact() { llint val = 1; fact[0] = 1; for(int i = 1; i < FACT_MAX; i++){ val *= i; val %= mod; fact[i] = val; } fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2); for(int i = FACT_MAX-2; i >= 0; i--){ fact_inv[i] = fact_inv[i+1] * (i+1) % mod; } } llint comb(llint n, llint k) { llint ret = 1; ret *= fact[n]; ret *= fact_inv[k], ret %= mod; ret *= fact_inv[n-k], ret %= mod; return ret; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); make_fact(); cin >> n >> m; rep(i, 1, n) cin >> p[i]; if(m == 1){ cout << 0 << endl; return 0; } rep(i, 1, n){ if(used[i]) continue; ll v = i, cnt = 0; while(!used[v]){ used[v] = true; cnt++; v = p[v]; } vec.push_back(cnt); } dp[0][0] = 1; rep(i, 1, n) rep(j, 1, m){ (dp[i][j] = dp[i-1][j-1] + j * dp[i-1][j] % mod) %= mod; } ll c = vec.size(); dp2[0][0][0] = 1; rep(i, 0, c-1){ rep(j, 0, n){ rep(k, 0, 1){ rep(l, 0, vec[i]-1){ if(j+l <= n) (dp2[i+1][j+l][(k+l)&1] += comb(vec[i], l) * dp2[i][j][k] % mod) %= mod; } if(j+vec[i] <= n) (dp2[i+1][j+vec[i]-1][(k+vec[i])&1] += dp2[i][j][k]) %= mod; } } } ll ans = 0; rep(i, 0, n){ ans += dp2[c][i][0] * dp[n-i][m] % mod, ans %= mod; ans += mod - dp2[c][i][1] * dp[n-i][m] % mod, ans %= mod; } cout << ans << endl; return 0; }