#include <bits/stdc++.h>
using namespace std;
vector<long long> facts, invfacts, pows, pows2;
vector<vector<long long>> makes;
long long k = 0;
long long extended_gcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long g = extended_gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return g;
}
long long nck(long long n, long long k) {
  if (k > n) return 0;
  return (facts[n] * invfacts[k] % 998244353 * invfacts[n - k] % 998244353 +
          998244353) %
         998244353;
}
void initialize_facts(long long n, long long mod) {
  facts.resize(n + 1);
  invfacts.resize(n + 1);
  pows.resize(n + 1);
  pows2.resize(n + 1);
  facts[0] = 1;
  invfacts[0] = 1;
  pows[0] = 1;
  pows2[0] = 1;
  for (long long i = 1; i <= n; i++) {
    facts[i] = (facts[i - 1] * i) % 998244353;
    long long x, y;
    extended_gcd(facts[i], mod, x, y);
    invfacts[i] = x;
    pows[i] = pows[i - 1] * (k - 2) % 998244353;
    pows2[i] = pows2[i - 1] * (k - 1) % 998244353;
  }
}
void make_matrix(long long n) {
  makes.assign(n + 1, vector<long long>(n + 1, 1));
  for (long long i = 1; i < (n + 1); i++) {
    for (long long j = 0; j < (i + 1); j++) {
      makes[i][j] = ((j - 1 >= 0 ? makes[i][j - 1] : 0) +
                     nck(i, j) * pows[i - j] % 998244353) %
                    998244353;
    }
  }
}
long long solve(long long n2, long long x) {
  if (x < 0) return 0;
  x = min(x, n2);
  return makes[n2][x];
}
int main() {
  long long(n);
  scanf("%lld", &(n));
  ;
  scanf("%lld", &k);
  initialize_facts(10000, 998244353);
  vector<long long> A((n));
  for (long long i = 0; i < n; i++) scanf("%lld", &A[i]);
  ;
  if (k == 1) {
    printf("0\n");
    return 0;
  }
  long long c1 = 0, c2 = 0;
  for (long long i = 0; i < (n); i++) {
    if (A[i] == A[(i + 1) % n]) c1++;
  }
  c2 = n - c1;
  make_matrix(n);
  long long res = 0;
  for (long long i = 0; i < (c1 + 1); i++) {
    for (long long j = 0; j < (c2 + 1); j++) {
      if (i == 0 && j == 0) continue;
      long long val = nck(c1, i) * nck(c2, j) % 998244353;
      res = (res + val * solve(c2 - j, j - 1) % 998244353 * pows2[c1 - i] %
                       998244353) %
            998244353;
    }
  }
  printf("%lld\n", res);
  return 0;
}
