#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T>
inline bool setmin(T &a, T b) {
  if (a > b) return a = b, 1;
  return 0;
}
template <class T>
inline bool setmax(T &a, T b) {
  if (a < b) return a = b, 1;
  return 0;
}
template <class T>
inline T fast(T a, T b, T mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
inline T russian(T a, T b, T mod) {
  long long res = 0;
  while (b) {
    if (b & 1) res = (res + a) % mod;
    a = (a + a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
istream &operator>>(istream &os, vector<T> &container) {
  for (auto &u : container) os >> u;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &container) {
  for (auto &u : container) os << u << " ";
  return os;
}
template <typename T>
inline T gcd(T a, T b) {
  while (b) swap(a %= b, b);
  return a;
}
const long long INF = 1e9 + 7;
const long long mod = 998244353;
const long long BIG_INF = 1e18 + 7;
const long long N = 1e6 + 7;
const int LOG = 17;
long long n, m, k, q;
long long sil[N];
long long odw[N];
long long pot[N];
long long pot_bez[N];
void prep() {
  sil[0] = 1;
  pot[0] = 1;
  pot_bez[0] = 1;
  for (int i = 1; i < N; i++) {
    sil[i] = sil[i - 1] * i % mod;
    pot[i] = pot[i - 1] * k % mod;
    pot_bez[i] = pot_bez[i - 1] * (k - 2) % mod;
  }
  odw[N - 1] = fast(sil[N - 1], mod - 2, mod);
  for (int i = N - 2; i >= 0; i--) {
    odw[i] = odw[i + 1] * (i + 1) % mod;
  }
}
long long n_po_k(long long a, long long b) {
  return (sil[a] * odw[b] % mod) * odw[a - b] % mod;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  cin >> k;
  if (k == 1) {
    cout << 0 << '\n';
    exit(0);
  }
  prep();
  vector<int> tab, pom;
  for (int i = 0; i < n; i++) {
    long long temp;
    cin >> temp;
    tab.push_back(temp);
  }
  pom.push_back(tab[0]);
  for (int i = 1; i < n; i++) {
    if (tab[i] != tab[i - 1]) {
      pom.push_back(tab[i]);
    }
  }
  if (pom[0] == pom.back() && (int)(pom).size() != 1) pom.pop_back();
  if ((int)(pom).size() == 1) {
    cout << 0 << '\n';
    exit(0);
  }
  long long wyn = 0;
  for (int i = 1; i <= (int)(pom).size(); i++) {
    long long temp = 0;
    for (int j = 0; j * 2 < i; j++) {
      temp = (temp + n_po_k(i, j) * pot_bez[(int)(pom).size() - i]) % mod;
    }
    temp = temp * n_po_k((int)(pom).size(), i) % mod;
    wyn = (wyn + temp) % mod;
  }
  wyn = wyn * pot[n - (int)(pom).size()] % mod;
  cout << wyn << '\n';
}
