#include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
long long dp[2005][4005];
int ans[2000];
int pow_mod(int a, int n, int m) {
  if (n == 0) return 1;
  int x = pow_mod(a, n / 2, m);
  long long ans = (long long)x * x % m;
  if (n % 2 == 1) ans = ans * a % m;
  return (int)ans;
}
int main() {
  int n, k;
  cin >> n >> k;
  int com = 0;
  for (int i = 0; i < n; i++) {
    cin >> ans[i];
    if (i != 0)
      if (ans[i] == ans[i - 1]) com++;
  }
  if (ans[n - 1] == ans[0]) com++;
  int dif = n - com;
  dp[1][dif] = k - 2;
  dp[1][dif - 1] = 1;
  dp[1][dif + 1] = 1;
  for (int i = 2; i <= dif; i++) {
    for (int j = 0; j <= 2 * dif; j++) {
      if (j - 1 >= 0) dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] %= mo;
      if (j + 1 <= 2 * dif) dp[i][j] += dp[i - 1][j + 1];
      dp[i][j] %= mo;
      dp[i][j] += dp[i - 1][j] * (k - 2);
      dp[i][j] %= mo;
    }
  }
  long long ans = 0;
  for (int i = dif + 1; i <= 2 * dif; i++) {
    ans += dp[dif][i];
    ans %= mo;
  }
  ans *= pow_mod(k, com, mo);
  ans %= mo;
  cout << ans;
}
