#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 998244353; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

int power(int n, int k){
	int res = 1;
	
	while(k){
		if(k&1) res *= n;
		
		k >>= 1;
		n *= n;
		
		n %= MOD;
		res %= MOD;
	}
	
	return res;
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, M;
	int ans = 0;
	vector<vector<int>> table;
	
	cin>>N>>M;
	
	table.resize(M+1, vector<int>(N+1));
	
	for(int i = 0; i < table.size(); i++){
		table[i][0] = 1;
		
		for(int j = 1; j < table[i].size(); j++){
			table[i][j] = table[i][j-1] * i % MOD;
		}
	}
	
	for(int h = 1; h <= M; h++){
		for(int j = 1; j < N-1; j++){
			int sum = 0;
			
			
			// sum = (power(M - h + 1, j) - power(M - h, j) + MOD) % MOD;
			sum = (table[M - h + 1][j] - table[M - h][j] + MOD) % MOD;
			sum = sum * (h - 1) % MOD;
			sum = (sum * table[M][N - j - 2]) % MOD;
			sum = sum * ((h - 1) * (N - j - 1) % MOD + M * 2) % MOD;
			
			ans = (ans + sum) % MOD;
		}
		
		if(N-1 > 0){
			int sum1 = 0, sum2 = 0;
			
			sum1 = (power(M - h + 1, N-1) - power(M - h, N-1) + MOD) % MOD;
			sum1 = (sum1 * (h - 1) % MOD) % MOD;
			sum1 = (MOD + sum1 * 2) % MOD;
			
			ans = (ans + sum1 + sum2) % MOD;
		}
		
		{
			int sum1 = 0, sum2 = 0;
			
			sum1 = (power(M - h + 1, N) - power(M - h, N) + MOD) % MOD;
			
			ans = (ans + sum1 + sum2) % MOD;
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 998244353
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}




int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    
    int N;
    cin >> N;
    
    ll ans = 1;
    
    vi f(N);
    in(N,f);
    rep(i,N) f[i]--;
    
    vi ff(N,0);
    
    int s = 0;
    rep(i,N){
        if(ff[i] == 1) continue;
        s = i;
        set<int> vis;
        int t = 0;
        
        while (ff[s] == 0) {
            ff[s] = 1;
            vis.insert(s);
            s = f[s];
            if(vis.count(s)){
                t = 1;
                break;
            }
        }
        
        if(t == 1) ans *= 2, ans %= mod;
        s = f[i];
        
    }
    
    cout << (ans+mod-1)%mod << endl;

    return 0;
}
