#include <bits/stdc++.h>
using namespace std;
int n, m, del, ans;
int a[4000 + 5], f[4000 + 5][4000 + 5];
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
signed main() {
  n = read(), m = read(), del = n;
  for (register int i = 1; i <= n; ++i) a[i] = read();
  if (m == 1) {
    puts("0");
    return 0;
  }
  f[0][del] = 1;
  for (register int i = 1; i <= n; ++i)
    for (register int j = -n; j <= n; ++j) {
      if (a[i] == a[i % n + 1])
        f[i][j + del] = 1ll * f[i - 1][j + del] * m % 998244353;
      else {
        f[i][j + del] = 1ll * (m - 2) * f[i - 1][j + del] % 998244353;
        if (j + del - 1 >= 0)
          f[i][j + del] = (f[i][j + del] + f[i - 1][j + del - 1]) % 998244353;
        if (j + del + 1 <= 2 * n)
          f[i][j + del] = (f[i][j + del] + f[i - 1][j + del + 1]) % 998244353;
      }
    }
  for (register int i = 1; i <= n; ++i) ans = (ans + f[n][i + del]) % 998244353;
  printf("%d", ans);
  return 0;
}
