#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;
const long long K = 2004;
long long c[K][K];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  long long n, k;
  cin >> n >> k;
  if (k == 1 || n == 1) {
    cout << 0;
    return 0;
  }
  memset(c, 0, sizeof c);
  for (long long i = 0; i < K - 1; ++i) {
    for (long long j = 0; j <= i; ++j) {
      if (j == 0) {
        c[i][j] = 1;
      } else {
        c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % M;
      }
    }
  }
  vector<long long> h;
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    h.push_back(x);
  }
  long long interesting = 0;
  for (long long i = 0; i < n; ++i) {
    if (h[i] != h[(i + 1) % n]) {
      interesting++;
    }
  }
  long long boring = n - interesting;
  long long base = 1;
  for (long long i = 0; i < boring; ++i) {
    base *= k;
    base %= M;
  }
  vector<long long> pows;
  pows.push_back(1);
  for (long long i = 1; i < 2004; ++i) {
    pows.push_back((pows[i - 1] * (k - 2)) % M);
  }
  long long res = 0;
  for (long long inc = 1; inc <= interesting; inc++) {
    for (long long dec = 0; dec < inc; ++dec) {
      long long con = interesting - inc - dec;
      if (con < 0) {
        break;
      }
      long long cur = base;
      cur *= c[interesting][inc];
      cur %= M;
      cur *= c[interesting - inc][dec];
      cur %= M;
      cur *= pows[con];
      cur %= M;
      res += cur;
      res %= M;
    }
  }
  cout << res;
}
