#include <bits/stdc++.h>
using namespace std;
const int N = 3005, Mod = 998244353;
int n, m, ans, a[N], f[N][N * 2];
inline void upd(int& x, int y) { x = (x + y >= Mod ? x + y - Mod : x + y); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  f[0][N] = 1;
  for (int i = 0; i < n; ++i)
    for (int j = -i; j <= i; ++j) {
      int k = N + j;
      int pos = i % n + 1;
      int ntps = pos % n + 1;
      if (f[i][k]) {
        if (a[pos] == a[ntps])
          upd(f[i + 1][k], (1ll * (f[i][k]) * (m) % Mod));
        else {
          if (m > 2) upd(f[i + 1][k], (1ll * (f[i][k]) * (m - 2) % Mod));
          upd(f[i + 1][k + 1], f[i][k]);
          upd(f[i + 1][k - 1], f[i][k]);
        }
      }
    }
  for (int i = 1; i <= n; ++i) upd(ans, f[n][N + i]);
  printf("%d\n", ans);
}
