#include using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mp make_pair #define pb push_back typedef long long ll; typedef long double ld; typedef pair Pii; typedef pair Pll; struct IOSetup { IOSetup() { cin.tie(nullptr)->sync_with_stdio(false); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); } } iosetup; template ostream &operator <<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator >>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator <<(ostream &os, const vector &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i+1 == (int) v.size() ? "" : " "); return os; } template istream &operator >>(istream &is, vector &v) { for(int i = 0; i < (int) v.size(); i++) is >> v[i]; return is; } template bool chmax(T &a, const T &b) { if(a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if(a > b) { a = b; return true; } return false; } //////////////////////////////////////////////////////// #include using mint = atcoder::modint998244353; istream &operator >>(istream &is, mint &a) { int x; is >> x; a = x; return is; } ostream &operator <<(ostream &os, const mint &a) { return os << a.val(); } int main() { int N, K; cin >> N >> K; vector> G(N); vector deg(N); for(int i = 0; i < N; i++) { int u, v; cin >> u >> v; u--; v--; G[u].pb(v); G[v].pb(u); deg[u]++; deg[v]++; } vector visited(N, false), cycle(N, true); auto dfs = [&](auto &self, int now) -> void { if(deg[now] != 1) return; cycle[now] = false; if(visited[now]) return; visited[now] = true; for(int nxt : G[now]) { if(visited[nxt]) continue; deg[nxt]--; if(deg[nxt] == 1) self(self, nxt); } }; for(int i = 0; i < N; i++) dfs(dfs, i); int cnt = accumulate(all(cycle), 0), res = N - cnt; auto solve = [](auto &self, int N, int M) -> mint { if(N == 2) return mint(M) * (M-1); else return mint(M) * mint(M-1).pow(N-1) - self(self, N-1, M); }; cout << mint(K-1).pow(res) * solve(solve, cnt, K) << endl; return 0; }