#include <bits/stdc++.h>
const int N = 1e6 + 5;
const int mod = 998244353;
using namespace std;
int p[105], cnt, h, r[N], g, gg, fac[N], inv[N], n, len, m, b[N], lim, k, a[N],
    sum, sum1, sum2, ans, tmp;
bool flag;
template <typename T>
inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * w;
}
int fpow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1) res = 1ll * res * x % mod;
  return res;
}
int getroot(int x) {
  int rem = x, tmp = x;
  for (int i = 2; i * i <= rem; i++)
    if (!(tmp % i)) {
      p[++cnt] = i;
      while (!(tmp % i)) tmp /= i;
    }
  if (tmp > 1) p[++cnt] = tmp;
  for (int flag = 1, i = 2; i <= x; i++, flag = 1) {
    for (int j = 1; j <= cnt && flag; j++)
      if (fpow(i, x / p[j]) == 1) flag = 0;
    if (flag) return i;
  }
  return -1;
}
void ntt(int *p, int opt) {
  for (int i = 0; i < h; i++)
    if (i < r[i]) swap(p[i], p[r[i]]);
  for (int i = 1; i < h; i <<= 1) {
    int rt = fpow(opt == 1 ? g : gg, (mod - 1) / (i << 1));
    for (int j = 0; j < h; j += (i << 1)) {
      int w = 1;
      for (int k = j; k < j + i; k++, w = 1ll * w * rt % mod) {
        int x = p[k], y = 1ll * w * p[k + i] % mod;
        p[k] = (x + y) % mod, p[k + i] = (x - y + mod) % mod;
      }
    }
  }
}
void solve() {
  g = getroot(mod - 1), gg = fpow(g, mod - 2);
  for (int i = (fac[0] = 1); i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = fpow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  len = n - m;
  for (int i = 0; i <= len; i++) b[i] = inv[i];
  for (len = 2 * len, h = 1; h <= len; h <<= 1, lim++)
    ;
  lim--;
  for (int i = 0; i < h; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << lim);
  ntt(b, 1);
  for (int i = 0; i < h; i++) b[i] = 1ll * b[i] * b[i] % mod;
  ntt(b, -1);
  gg = fpow(h, mod - 2);
  for (int i = 0; i < h; i++) b[i] = 1ll * b[i] * gg % mod;
}
int main() {
  n = read<int>(), k = read<int>();
  for (int i = 1; i <= n; i++) a[i] = read<int>();
  if (k == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) m += (a[i] == a[i % n + 1]);
  solve();
  sum = 1ll * fpow(k, m) * fac[n - m] % mod;
  for (int i = 0; i * 2 <= n - m; i++)
    sum1 = (sum1 + 1ll * inv[i] * inv[i] % mod * inv[n - m - 2 * i] % mod *
                       fpow(k - 2, n - m - 2 * i) % mod) %
           mod;
  gg = fpow(2, mod - 2);
  for (int i = 0; i <= n - m; i++)
    tmp = (tmp +
           1ll * b[i] * inv[n - m - i] % mod * fpow(k - 2, n - m - i) % mod) %
          mod;
  ans = 1ll * sum % mod * (tmp - sum1 + mod) % mod * gg % mod;
  printf("%d\n", ans);
  return 0;
}
