#include <bits/stdc++.h>
inline int read() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    ch = getchar();
    if (ch == '-') w = -1;
  }
  while (ch <= '9' && ch >= '0') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * w;
}
using namespace std;
const int N = 5e5 + 5;
const int Mod = 998244353;
inline int Pow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = 1ll * res * x % Mod;
    x = 1ll * x * x % Mod;
    y >>= 1;
  }
  return res;
}
inline void ad(int &x, int y) {
  x += y;
  if (x >= Mod) x -= Mod;
  if (x < 0) x += Mod;
}
int n, k, cnt, ans;
int a[N], b[N];
int fac[N], inv[N];
inline void math_init(int nn) {
  fac[0] = 1;
  for (int i = 1; i <= nn; ++i) fac[i] = 1ll * fac[i - 1] * i % Mod;
  inv[nn] = Pow(fac[nn], Mod - 2);
  for (int i = nn; i; --i) inv[i - 1] = 1ll * inv[i] * i % Mod;
}
inline int C(int x, int y) {
  if (x < y || x < 0 || y < 0) return 0;
  return 1ll * (1ll * fac[x] * inv[y] % Mod) * inv[x - y] % Mod;
}
int main() {
  n = read(), k = read();
  math_init(n);
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) b[i] = a[i % n + 1];
  for (int i = 1; i <= n; ++i)
    if (a[i] ^ b[i]) ++cnt;
  if (!cnt) return puts("0"), 0;
  for (int i = 0; i <= cnt; ++i) {
    int now = 1ll * Pow(k - 2, i) * Pow(k, n - cnt) % Mod;
    int p = Pow(2, cnt - i);
    if ((cnt - i) % 2 == 0) ad(p, Mod - C(cnt - i, (cnt - i) >> 1));
    p = 1ll * p * ((Mod + 1) >> 1) % Mod;
    now = 1ll * now * C(cnt, i) % Mod;
    ad(ans, 1ll * p * now % Mod);
  }
  printf("%d\n", ans);
  return 0;
}
