#include using namespace std; long long mod = 998244353; //入力が必ず-mod= 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++(){*this += 1; return *this;} mint operator--(){*this -= 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(long long n){ mint ret = 1,p = v; if(n < 0) p = p.inv(),n = -n; while(n){ if(n&1) ret *= p; p *= p; n >>= 1; } return ret; } mint inv(){return mint(1)/v;} }; mint jury(int N,int M,vector A){ vector B(N); mint ret = 0; auto dfs = [&](auto dfs,int pos) -> void { if(pos == N){ for(int i=0; iA.at(i)); for(int i=0; iA.at(i); one += B.at(i+1)>A.at(i+1); int two = B.at(i)>A.at(i+1); two += B.at(i+1)>A.at(i); if(one < two){ret++; break;} } return; } for(int i=1; i<=M; i++){ B.at(pos) = i; dfs(dfs,pos+1); } }; dfs(dfs,0); return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector A(N); for(auto &a : A) cin >> a; //cout << jury(N,M,A).v << endl; vector pm(N+1,1),S(N); for(int i=1; i<=N; i++) pm.at(i) = pm.at(i-1)*M; for(int i=N-1; i>=0; i--){ S.at(i) = (M-A.at(i))%mod; if(i < N-1) S.at(i) += S.at(i+1); } mint answer = pm.at(N-1)*(M-A.at(0)); vector> imos = {{1,1}}; int a2 = A.at(0); for(int i=1; i> next; int a = A.at(i); if(a2 <= a){ mint now = 0; { int back = 0; mint sum = 0; for(auto [p,v] : imos){ now += sum*(p-back); sum += v; back = p; } now += sum*(M+1-back); } answer += now*(M-A.at(i))*pm.at(N-1-i); next.push_back({1,now}); mint end = 0; { int back = 0; mint sum = 0; for(auto [p,v] : imos){ end += sum*max(0,min(p,a+1)-max(back,a2+1)); sum += v; back = p; } end += sum*max(0,min(M+1,a+1)-max(back,a2+1)); } mint plus1 = (now-end)*(a-a2); if(i < N-1) plus1 = plus1*pm.at(N-1-i)+plus1*pm.at(N-2-i)*S.at(i+1); else plus1 = plus1*pm.at(N-1-i); answer += plus1; next.push_back({a2+1,-now+end}); next.push_back({a+1,now-end}); } else{ swap(a,a2); mint now = 0; { int back = 0; mint sum = 0; for(auto [p,v] : imos){ now += sum*(p-back); sum += v; back = p; } now += sum*(M+1-back); } answer += now*(M-A.at(i))*pm.at(N-1-i); next.push_back({1,now}); mint end = 0; { int back = 0; mint sum = 0; for(auto [p,v] : imos){ end += sum*max(0,min(p,a+1)-max(back,a2+1)); sum += v; back = p; } end += sum*max(0,min(M+1,a+1)-max(back,a2+1)); } mint plus1 = end*(M-(a-a2)); if(i < N-1) plus1 = plus1*pm.at(N-1-i)+plus1*pm.at(N-2-i)*S.at(i+1); else plus1 = plus1*pm.at(N-1-i); answer += plus1; next.push_back({1,-end}); next.push_back({a2+1,end}); next.push_back({a+1,-end}); } a2 = A.at(i); imos = next; } cout << answer.v << endl; }