#include <bits/stdc++.h>
using namespace std;
vector<string> vec_splitter(string s) {
  for (char& c : s) c = c == ',' ? ' ' : c;
  stringstream ss;
  ss << s;
  vector<string> res;
  for (string z; ss >> z; res.push_back(z))
    ;
  return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
  if (idx > 0) cerr << ", ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, T...);
}
void localTest() {}
const long long N = 2e3 + 5, MOD = 998244353, offset = N;
long long dp[N][2 * N], A[N];
int main() {
  localTest();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, k, ans = 0;
  cin >> n >> k;
  for (long long i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  if (k == 1) {
    cout << "0\n";
    return 0;
  }
  dp[0][offset] = 1;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = -i; j <= i; ++j) {
      if (A[i] == A[i % n + 1]) {
        dp[i][offset + j] = (dp[i - 1][offset + j] * k) % MOD;
      } else {
        dp[i][offset + j] = (dp[i - 1][offset + j + 1] +
                             (dp[i - 1][offset + j] * (k - 2)) % MOD) %
                            MOD;
        dp[i][offset + j] += dp[i - 1][offset + j - 1] % MOD;
      }
    }
  }
  for (long long j = 1; j <= n; ++j) {
    ans = (ans + dp[n][offset + j]) % MOD;
  }
  cout << ans << "\n";
  return 0;
}
