#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
vector<int> h;
vector<vector<int> > c;
vector<int> p;
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  h.resize(n);
  p.assign(n + 1, 1);
  for (int i = 0; i < n; ++i) {
    p[i + 1] = p[i] * (long long)(k - 2) % mod;
  }
  c.assign(n + 1, vector<int>(n + 1, 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < i; ++j) {
      c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  int eq = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (h[i] == h[i + 1]) {
      eq++;
    }
  }
  if (h[0] == h[n - 1]) {
    eq++;
  }
  n -= eq;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < i && i + j <= n; ++j) {
      long long cur = (long long)(c[n][i]) * c[n - i][j] % mod;
      ans += cur * p[n - i - j] % mod;
      ans %= mod;
    }
  }
  for (int i = 0; i < eq; ++i) {
    ans *= k;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
