#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e3 + 5;
const int mod = 998244353;
int n, k, a[maxn];
int den, cnt;
ll f[maxn], inv[maxn];
ll ans;
ll binmod(ll a, ll b) {
  if (b == 0) return 1;
  ll x = binmod(a * a % mod, b >> 1);
  return b & 1 ? x * a % mod : x;
}
ll choose(int a, int b) {
  if (a < b) return 0;
  return (((f[a] * inv[b]) % mod) * inv[a - b]) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  if (k == 1) return cout << 0, 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  f[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) f[i] = (f[i - 1] * i) % mod;
  inv[n] = binmod(f[n], mod - 2);
  for (int i = n - 1; i >= 1; i--) inv[i] = (inv[i + 1] * (i + 1)) % mod;
  for (int i = 1; i <= n; i++) {
    int ind = i % n + 1;
    if (a[i] == a[ind])
      den++;
    else
      cnt++;
  }
  ll w = binmod(k, den);
  for (int i = 1; i <= cnt; i++) {
    ll fr = choose(cnt, i);
    for (int j = 0; j < i; j++) {
      int lft = cnt - i - j;
      if (lft < 0) continue;
      ll sc = choose(cnt - i, j);
      ll num = (fr * sc) % mod;
      num = (num * binmod(k - 2, lft)) % mod;
      num = (num * w) % mod;
      ans = (ans + num) % mod;
    }
  }
  cout << ans;
}
