#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
template <int MOD = 998244353>
struct Modular {
  int value;
  static const int MOD_value = MOD;
  Modular(long long v = 0) {
    value = v % MOD;
    if (value < 0) value += MOD;
  }
  Modular(long long a, long long b) : value(0) {
    *this += a;
    *this /= b;
  }
  Modular& operator+=(Modular const& b) {
    value += b.value;
    if (value >= MOD) value -= MOD;
    return *this;
  }
  Modular& operator-=(Modular const& b) {
    value -= b.value;
    if (value < 0) value += MOD;
    return *this;
  }
  Modular& operator*=(Modular const& b) {
    value = (long long)value * b.value % MOD;
    return *this;
  }
  friend Modular mexp(Modular a, long long e) {
    Modular res = 1;
    while (e) {
      if (e & 1) res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {
    return os << a.value;
  }
  friend bool operator==(Modular const& a, Modular const& b) {
    return a.value == b.value;
  }
  friend bool operator!=(Modular const& a, Modular const& b) {
    return a.value != b.value;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int n, k, y = 0;
  cin >> n >> k;
  if (k == 1 || n == 1) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (i && a[i] == a[i - 1]) y++;
  }
  if (a[n - 1] == a[0]) y++;
  int x = n - y;
  Modular<> ans = 0, fact[3 * N], inv[3 * N];
  fact[0] = inv[0] = 1;
  for (int i = 1; i < 3 * N; ++i) {
    fact[i] = i * fact[i - 1];
    inv[i] = 1 / fact[i];
  }
  auto ncr = [&](int nn, int rr) {
    Modular<> ret = fact[nn] * inv[rr] * inv[nn - rr];
    return ret;
  };
  for (int i = 1; i <= x; ++i) {
    Modular<> cur = ncr(x, i), tmp = 0;
    int rem = x - i;
    for (int j = 0; j <= min(i - 1, rem); ++j) {
      tmp += ncr(rem, j) * mexp(Modular<>(k - 2), rem - j);
    }
    ans += cur * tmp;
  }
  ans *= mexp(Modular<>(k), y);
  cout << ans << '\n';
}
