#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 998244353;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x * f;
}
int inc(int x, int y) { return (x + y >= mod) ? (x + y - mod) : (x + y); }
int main() {
  int n = read(), k = read();
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> a(n + 2);
  vector<vector<int>> dp(n + 2, vector<int>(2 * n + 2));
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  a[n + 1] = a[1];
  dp[0][n] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 2 * n; j++) {
      if (a[i] == a[i + 1])
        dp[i][j] = 1ll * k * dp[i - 1][j] % mod;
      else {
        dp[i][j] = inc(dp[i][j], dp[i - 1][j - 1]);
        dp[i][j] = inc(dp[i][j], dp[i - 1][j + 1]);
        dp[i][j] = inc(dp[i][j], 1ll * (k - 2) * dp[i - 1][j] % mod);
      }
    }
  }
  int res = 0;
  for (int i = n + 1; i <= 2 * n; i++) res = inc(res, dp[n][i]);
  cout << res << endl;
}
