#include <bits/stdc++.h>
using namespace std;
char buf[1 << 21], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline int read() {
  int x = 0, sign = 0;
  char s = getchar();
  while (!isdigit(s)) sign |= s == '-', s = getchar();
  while (isdigit(s)) x = (x << 1) + (x << 3) + (s - '0'), s = getchar();
  return sign ? -x : x;
}
void print(int x) {
  if (x > 9) print(x / 10);
  (*O++ = x % 10 + '0');
}
const int N = 2e3 + 5;
const long long mod = 998244353;
long long n, k, h[N], f[N][N << 1];
int main() {
  cin >> n >> k, f[0][n] = 1;
  for (int i = 1; i <= n; i++) cin >> h[i];
  for (int i = 1; i <= n; i++) {
    int prev = (i == 1 ? h[n] : h[i - 1]);
    for (int j = 1; j <= n * 2; j++) {
      if (prev != h[i])
        f[i][j] =
            (f[i - 1][j] * (k - 2) + f[i - 1][j - 1] + f[i - 1][j + 1]) % mod;
      else
        f[i][j] = f[i - 1][j] * k % mod;
    }
  }
  long long ans = 0;
  for (int i = n + 1; i <= n * 2; i++) ans = (ans + f[n][i]) % mod;
  cout << ans << endl;
  return 0;
}
