#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 1;
const long long mod = 998244353;
int n, a[N], k;
long long dp[N][2 * N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (k == 1)
    cout << 0 << '\n';
  else {
    if (a[1] == a[1 % n + 1])
      dp[1][n] = k;
    else {
      dp[1][n - 1] = 1;
      dp[1][n + 1] = 1;
      dp[1][n] = k - 2;
    }
    for (int i = 2; i <= n; i++) {
      if (a[i] == a[i % n + 1]) {
        for (int j = 0; j <= 2 * n; j++) {
          dp[i][j] = (dp[i - 1][j] * (long long)k) % mod;
        }
      } else {
        for (int j = 0; j <= 2 * n; j++) {
          dp[i][j] = (dp[i][j] + dp[i - 1][j] * (long long)(k - 2) % mod) % mod;
          if (j > 0) dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
          if (j < 2 * n) dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod;
        }
      }
    }
    long long ans = 0;
    for (int i = n + 1; i <= 2 * n; i++) ans = (ans + dp[n][i]) % mod;
    cout << ans << '\n';
  }
}
