#include <bits/stdc++.h>
using namespace std;
const long long MN = 2007;
const long long mod = 998244353;
long long a[MN];
long long dp[MN][MN * 2];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  long long n, k;
  cin >> n >> k;
  {
    for (long long i = 0; i < n; i++) cin >> a[i];
  };
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j <= n * 2; j++) {
      long long nw = a[i - 1];
      long long nx = a[i % n];
      if (i == 1) {
        dp[i][j] = 0;
        if (nw == nx) {
          if (j > n)
            dp[i][j] = k;
          else
            dp[i][j] = 0;
        } else {
          if (j > n) dp[i][j] = k - 1;
          if (j > n + 1) dp[i][j] = k;
          if (j == n) dp[i][j] = 1;
        }
        continue;
      }
      if (nw == nx) {
        dp[i][j] = dp[i - 1][j] * k % mod;
        continue;
      }
      dp[i][j] = dp[i - 1][j] * (k - 2) % mod;
      if (j) dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
      if (j < n * 2) dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod;
      dp[i][j] %= mod;
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j <= 2 * n; j++) {
    }
  }
  cout << dp[n][n];
}
