#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long k;
  cin >> n >> k;
  vector<vector<long long>> dp(n, vector<long long>(2 * n + 1));
  if (k == 1) {
    cout << 0;
    return 0;
  }
  vector<int> h(n);
  for (int &i : h) cin >> i;
  vector<long long> kpow(n + 1), km2pow(n + 1);
  kpow[0] = 1, km2pow[0] = 1;
  for (int i = 1; i <= n; i++) {
    kpow[i] = kpow[i - 1] * k;
    kpow[i] %= MOD;
    km2pow[i] = km2pow[i - 1] * (k - 2);
    km2pow[i] %= MOD;
  }
  vector<vector<long long>> ncr(n + 1, vector<long long>(n + 1, 0));
  ncr[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    ncr[i][0] = 1;
    for (int r = 1; r <= i; r++) {
      ncr[i][r] = ncr[i - 1][r - 1] + ncr[i - 1][r];
      ncr[i][r] %= MOD;
    }
  }
  int s = 0;
  for (int i = 0; i < n; i++) {
    if (h[i] != h[(i + 1) % n]) s++;
  }
  long long ans = 0;
  for (int t = 1; t <= s; t++) {
    for (int j = 0; j < t && t + j <= s; j++) {
      long long val = ncr[s][t] * ncr[s - t][j];
      val %= MOD;
      val *= km2pow[s - t - j];
      val %= MOD;
      ans += val;
      ans %= MOD;
    }
  }
  ans *= kpow[n - s];
  ans %= MOD;
  cout << ans;
  return 0;
}
