#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
signed main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> h(n);
  for (long long i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<long long> pw(n + 1);
  vector<long long> pw2(n + 1);
  pw[0] = 1;
  pw2[0] = 1;
  for (long long i = 1; i <= n; i++) {
    pw[i] = (pw[i - 1] * k) % MOD;
    pw2[i] = (pw2[i - 1] * (k - 2)) % MOD;
  }
  vector<vector<long long> > c(n + 1, vector<long long>(n + 1));
  c[0][0] = 1;
  for (long long i = 1; i <= n; i++) {
    c[i][0] = 1;
    for (long long j = 1; j <= n; j++) {
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
    }
  }
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    if (h[i] != h[(i + 1) % n]) {
      cnt++;
    }
  }
  long long r = pw[n - cnt];
  long long ans = 0;
  for (long long a = 1; a <= cnt; a++) {
    for (long long b = 0; b < a; b++) {
      if (a + b > cnt) {
        continue;
      }
      long long cur = (c[cnt][a] * c[cnt - a][b]) % MOD;
      cur = (cur * pw2[cnt - a - b]) % MOD;
      ans = (ans + cur) % MOD;
    }
  }
  ans = (ans * r) % MOD;
  cout << ans << endl;
  return 0;
}
