#include <bits/stdc++.h>
using namespace std;
unsigned long long binexp(unsigned long long base, unsigned long long pow,
                          unsigned long long M) {
  unsigned long long res = 1;
  while (pow) {
    if (pow & 1) res *= base;
    pow >>= 1, base *= base;
    if (M) base %= M, res %= M;
  }
  return res;
}
vector<long long> fac(2e5 + 10);
long long nCk(long long n, long long k) {
  return (fac[n] *
          binexp((fac[k] * fac[n - k]) % 998244353, 998244353 - 2, 998244353)) %
         998244353;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  fac[0] = 1;
  for (long long x = 1; x <= 2e5; x++) fac[x] = (x * fac[x - 1]) % 998244353;
  long long n, k;
  cin >> n >> k;
  vector<long long> h(n);
  for (auto &i : h) cin >> i;
  long long cnt[2] = {};
  for (long long x = 0; x < n; x++) {
    if (h[x] == h[(x + 1) % n])
      cnt[0]++;
    else
      cnt[1]++;
  }
  long long ans = binexp(k, cnt[0], 998244353);
  long long ncnt = 0;
  for (long long x = 0; x <= cnt[1]; x++) {
    for (long long i = 0; i < x; i++) {
      long long j = cnt[1] - x - i;
      if (j < 0) break;
      ncnt += (((nCk(cnt[1], x) * nCk(cnt[1] - x, i)) % 998244353) *
               binexp(k - 2, j, 998244353)) %
              998244353;
      ncnt %= 998244353;
    }
  }
  cout << (ans * ncnt) % 998244353 << endl;
  return 0;
}
