#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005, maxm = 512;
const double pi = acos(-1.0);
const long long mod = 998244353LL;
const double eps = 1e-8;
long long ksm(long long a, long long k) {
  long long r = 1;
  while (k > 0) {
    if (k & 1) r *= a, r %= mod;
    a = a * a % mod;
    k >>= 1;
  }
  return r;
}
int n, k, m;
int a[maxn], b[maxn];
long long fa[maxn], fi[maxn], iv2;
int main() {
  iv2 = ksm(2, mod - 2);
  fa[0] = fi[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    fa[i] = fa[i - 1] * i % mod;
    fi[i] = ksm(fa[i], mod - 2);
  }
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    b[(i - 1 + n) % n] = a[i];
  }
  if (k == 1) {
    printf("0\n");
    return 0;
  }
  m = 0;
  for (int i = 0; i < n; ++i) {
    m += (a[i] != b[i]);
  }
  long long ans = 0;
  for (int i = 0; i <= m; ++i) {
    long long tt = ksm(k - 2, i) * fa[m] % mod * fi[i] % mod * fi[m - i] % mod;
    if ((m - i) % 2) {
      tt *= ksm(2, m - i - 1);
      tt %= mod;
    } else {
      long long ss = ksm(2, m - i) + mod -
                     fa[m - i] * fi[(m - i) / 2] % mod * fi[(m - i) / 2] % mod;
      ss %= mod;
      ss *= iv2;
      ss %= mod;
      tt *= ss;
      tt %= mod;
    }
    ans += tt;
    ans %= mod;
  }
  ans *= ksm(k, n - m);
  ans %= mod;
  printf("%lld\n", ans);
  return 0;
}
