#include typedef struct list { int v; struct list *n; } list; int func (int v, int prev, int tmp_d, list **e, int *d) { list *l = e[v]; if (d[v] > 0) { return tmp_d-d[v]; } d[v] = tmp_d; while (l != NULL) { if (l->v != prev) { int res = func(l->v, v, tmp_d+1, e, d); if (res > 0) { return res; } } l = l->n; } return 0; } int main () { int n = 0; int k = 0; int u = 0; int v = 0; int res = 0; long long ans = 0LL; long long mod_num = 998244353LL; list *e[200000] = {}; list pool[400000] = {}; int used = 0; int d[200000] = {}; int len = 0; long long dp[200000][2] = {}; res = scanf("%d", &n); res = scanf("%d", &k); for (int i = 0; i < n; i++) { res = scanf("%d", &u); res = scanf("%d", &v); u--; v--; pool[used].v = v; pool[used].n = e[u]; e[u] = pool+used; used++; pool[used].v = u; pool[used].n = e[v]; e[v] = pool+used; used++; } len = func(0, -1, 1, e, d); dp[0][0] = ((long long)k); dp[0][1] = 0LL; for (int i = 1; i < len; i++) { dp[i][0] = dp[i-1][1]; dp[i][1] = dp[i-1][0]*((long long)(k-1))+dp[i-1][1]*((long long)(k-2)); dp[i][1] %= mod_num; } ans = dp[len-1][1]; for (int i = 0; i < n-len; i++) { ans *= ((long long)(k-1)); ans %= mod_num; } printf("%lld\n", ans); return 0; }