#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << '(' << a.first << ", " << a.second << ')';
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  for (unsigned long long i = 0; i < a.size(); i++)
    os << a[i] << (i < a.size() - 1 ? ", " : "");
  os << ']';
  return os;
}
const long long mod = 998244353;
long long exp__[N];
long long binPow(long long a, long long q) {
  long long ret = 1;
  while (q) {
    if (q & 1) ret = 1LL * ret * a % mod;
    a = 1LL * a * a % mod;
    q >>= 1LL;
  }
  return ret;
}
long long div__(long long X, long long Y) {
  return X * binPow(Y, mod - 2) % mod;
}
long long n, m;
long long avail = 0;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  exp__[0] = 1;
  for (long long i = (1); i <= (N - 5); i++) exp__[i] = exp__[i - 1] * i % mod;
  cin >> n >> m;
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  vector<long long> a;
  for (long long i = (0); i <= (n - 1); i++) {
    long long t;
    cin >> t;
    a.push_back(t);
  }
  long long free = 0;
  for (long long i = (0); i <= (n - 1); i++) {
    if (a[i] != a[(i + 1) % n])
      avail++;
    else
      free++;
  }
  long long ans = 0;
  for (long long i = (0); i <= (avail); i++) {
    long long j = i;
    if (i + j > avail) break;
    ans +=
        div__(1, 1LL * exp__[i] * exp__[j] % mod * exp__[avail - i - j] % mod) *
        binPow(m - 2, avail - i - j) % mod;
    ans %= mod;
  }
  ans = ans * binPow(m, free) % mod * exp__[avail] % mod;
  ans = div__(binPow(m, n) - ans, 2);
  cout << (ans % mod + mod) % mod << '\n';
}
