#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; VI a(n); rep(i, n) cin >> a[i], a[i]--; mint ans; rep(i, n) ans += m - (a[i] + 1); ans *= mint(m).pow(n - 1); ans += mint(m).pow(n); vector

lr(n + 1); for (int i = 1; i < n; i++) { lr[i] = {min(a[i-1], a[i]) + 1, max(a[i-1], a[i]) + 1}; } vector> ps(n); rep(i, n) { ps[i][1] = lr[i].first; ps[i][2] = lr[i].second; ps[i][3] = lr[i+1].first; ps[i][4] = lr[i+1].second; ps[i][5] = m; ranges::sort(ps[i]); } array dp; auto is_between = [&](int x, P rng) { return rng.first <= x && x < rng.second; }; rep(j, 5) dp[j] = ps[0][j+1] - ps[0][j]; for (int i = 1; i < n; i++) { array ndp; rep(j, 5) rep(k, 5) { if ((a[i-1] > a[i] && is_between(ps[i-1][j], lr[i]) && !is_between(ps[i][k], lr[i])) || (a[i-1] < a[i] && is_between(ps[i][k], lr[i]) && !is_between(ps[i-1][j], lr[i]))) { continue; } ndp[k] += dp[j] * (ps[i][k+1] - ps[i][k]); } dp = ndp; } mint bad = accumulate(all(dp), mint()); ans -= bad; cout << ans.val() << '\n'; }