#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = (int)2e3 + 5;
const int MOD = 998244353;
int dp[N][2 * N + 1];
int h[N];
int q[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
    q[(i + 1) % n] = h[i];
  }
  int zero = n;
  dp[0][zero] = 1;
  for (int i = 0; i < n; ++i) {
    for (int bal = zero - i; bal <= zero + i; ++bal) {
      if (h[i] != q[i]) {
        dp[i + 1][bal] =
            (int)((dp[i + 1][bal] + (1LL * (k - 2) * dp[i][bal])) % MOD);
        dp[i + 1][bal - 1] += dp[i][bal];
        dp[i + 1][bal - 1] %= MOD;
        dp[i + 1][bal + 1] += dp[i][bal];
        dp[i + 1][bal + 1] %= MOD;
      } else {
        dp[i + 1][bal] = (int)((dp[i + 1][bal] + (1LL * k * dp[i][bal])) % MOD);
      }
    }
  }
  int ans = 0;
  for (int bal = zero + 1; bal <= zero + n; ++bal) {
    ans = (ans + dp[n][bal]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
