#include <bits/stdc++.h>
using namespace std;
template <typename T = long long>
T input() {
  T res;
  cin >> res;
  return res;
}
const long long N = 2007, MOD = 998244353;
long long fact[N], anti[N];
long long pw(long long x, long long d) {
  if (d == 0) {
    return 1;
  }
  if (d % 2 == 0) {
    return pw((x * x) % MOD, d / 2);
  }
  return (x * pw(x, d - 1)) % MOD;
}
long long c(long long n, long long k) {
  return (((fact[n] * anti[k]) % MOD) * anti[n - k]) % MOD;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  fact[0] = 1;
  anti[0] = 1;
  for (long long i = 1; i < N; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
    anti[i] = (anti[i - 1] * pw(i, MOD - 2)) % MOD;
  }
  long long n = input();
  long long k = input();
  vector<long long> h(n);
  for (long long &i : h) {
    i = input();
  }
  long long ans = 0, ml = 1, tmp = 0;
  for (long long i = 0; i < n; ++i) {
    long long j = (i + 1) % n;
    if (h[i] == h[j]) {
      ml *= k;
      ml %= MOD;
    } else {
      ++tmp;
    }
  }
  for (long long i = 0; i <= tmp; ++i) {
    for (long long j = 0; j <= tmp - i; ++j) {
      if (i > j) {
        ans += c(tmp, i) * c(tmp - i, j) % MOD * pw(k - 2, tmp - i - j) % MOD;
        ans %= MOD;
      }
    }
  }
  cout << (ans * ml) % MOD << endl;
}
