#include <bits/stdc++.h>

using namespace std;
#define int long long
const int p=998244353;
int po(int a,int b) {if(b==0) return 1;if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*u)%p;} else {int u=po(a,b-1);return (a*u)%p;}}
const int maxn=2e5+5;
vector<int> a[maxn];bool used[maxn];int corn[maxn];int cn=0;
void dfs(int x,int pr)
{
    used[x]=true;
    for(int v:a[x])
    {
        if(!used[v])
        {
            corn[v]=corn[x]+1;
            dfs(v,x);
        }
        else if(v!=pr && corn[v]<corn[x])
        {
            cn=corn[x]-corn[v]+1;
        }
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;cin>>n>>k;
    for(int i=0;i<n;++i)
    {
        int x,y;cin>>x>>y;--x;--y;
        a[x].push_back(y);a[y].push_back(x);
    }
    dfs(0,-1);
    int res=po(k-1,cn);if(cn%2==0) {res+=(k-1);} else {res-=(k-1);}
    res%=p;
    res*=po(k-1,n-cn);res%=p;
    cout<<(res%p+p)%p;
    return 0;
}