#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 1;
const long long MOD = 998244353;
const long long MAXN = 1e6 + 5;
const long double EPS = 1e-12;
const long double PI = acos(-1);
const long long XX[] = {0, -1, 0, 1, 0, -1};
const long long YY[] = {-1, 0, 1, 0, -1, 0};
const long long ALPH = 31;
const long long LEN = 20;
void bye() {
  cout << "\n";
  exit(0);
}
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> b(n);
  for (long long i = 0; i < n; i++) {
    b[i] = a[(i + 1) % n];
  }
  vector<vector<long long> > dp(n, vector<long long>(2 * (n + 2)));
  const long long STEP = n + 2;
  if (a[0] == b[0]) {
    dp[0][0 + STEP] = k;
  } else {
    dp[0][0 + STEP] = k - 2;
    dp[0][1 + STEP] = 1;
    dp[0][-1 + STEP] = 1;
  }
  for (long long i = 1; i < n; i++) {
    for (long long j = -n; j <= n; j++) {
      if (a[i] == b[i]) {
        dp[i][j + STEP] = ((dp[i - 1][j + STEP] * k)) % MOD;
      } else {
        dp[i][j + STEP] = ((dp[i - 1][j + STEP] * (k - 2))) % MOD;
        dp[i][j + STEP] += dp[i - 1][j + 1 + STEP];
        dp[i][j + STEP] %= MOD;
        dp[i][j + STEP] += dp[i - 1][j - 1 + STEP];
        dp[i][j + STEP] %= MOD;
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    ans += dp[n - 1][i + STEP];
    ans %= MOD;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long TEST_COUNT = 1;
  while (TEST_COUNT--) {
    solve();
  }
  bye();
}
