#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
struct modint {
  int x;
  modint(int o = 0) { x = o; }
  modint &operator=(int o) { return x = o, *this; }
  modint &operator+=(modint o) {
    return x = x + o.x >= 998244353 ? x + o.x - 998244353 : x + o.x, *this;
  }
  modint &operator-=(modint o) {
    return x = x - o.x < 0 ? x - o.x + 998244353 : x - o.x, *this;
  }
  modint &operator*=(modint o) { return x = 1ll * x * o.x % 998244353, *this; }
  modint &operator^=(int b) {
    modint a = *this, c = 1;
    for (; b; b >>= 1, a *= a)
      if (b & 1) c *= a;
    return x = c.x, *this;
  }
  modint &operator/=(modint o) { return *this *= o ^= 998244353 - 2; }
  modint &operator+=(int o) {
    return x = x + o >= 998244353 ? x + o - 998244353 : x + o, *this;
  }
  modint &operator-=(int o) {
    return x = x - o < 0 ? x - o + 998244353 : x - o, *this;
  }
  modint &operator*=(int o) { return x = 1ll * x * o % 998244353, *this; }
  modint &operator/=(int o) { return *this *= ((modint(o)) ^= 998244353 - 2); }
  template <class I>
  friend modint operator+(modint a, I b) {
    return a += b;
  }
  template <class I>
  friend modint operator-(modint a, I b) {
    return a -= b;
  }
  template <class I>
  friend modint operator*(modint a, I b) {
    return a *= b;
  }
  template <class I>
  friend modint operator/(modint a, I b) {
    return a /= b;
  }
  friend modint operator^(modint a, int b) { return a ^= b; }
  friend bool operator==(modint a, int b) { return a.x == b; }
  friend bool operator!=(modint a, int b) { return a.x != b; }
  bool operator!() { return !x; }
  modint operator-() { return x ? 998244353 - x : 0; }
};
int n, m, a[2000005];
modint fac[2000005], ifac[2000005], k;
inline void prework(int n) {
  fac[0] = ifac[0] = 1;
  for (register int i = (1); i <= (n); ++i)
    fac[i] = fac[i - 1] * i, ifac[i] = ifac[i - 1] / i;
}
modint C(int n, int m) { return fac[n] * ifac[m] * ifac[n - m]; }
signed main() {
  n = read(), k = read();
  for (register int i = (1); i <= (n); ++i) a[i] = read();
  m = (a[1] != a[n]);
  for (register int i = (2); i <= (n); ++i) m += (a[i] != a[i - 1]);
  swap(n, m), prework(n);
  modint pw2 = 1, res = 0;
  for (register int i = (0); i <= (n); ++i) {
    res +=
        C(n, i) * ((k - 2) ^ (n - i)) * (pw2 - C(i, i / 2) * (i % 2 == 0)) / 2;
    pw2 *= 2;
  }
  res *= (k ^ (m - n));
  cout << res.x;
  return 0;
}
