#include <bits/stdc++.h>

#define LL long long
#define ull unsigned long long
#define F(i, j, k) for (int i = j; i <= k; ++i)
#define DF(i, j, k) for (int i = j; i >= k; --i)

using namespace std;

template <typename T> inline void read(T &n) {
    T w = 1;
    n = 0;
    char ch = getchar();
    while (!isdigit(ch) && ch != EOF) {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (isdigit(ch) && ch != EOF) {
        n = (n << 3) + (n << 1) + (ch & 15);
        ch = getchar();
    }
    n *= w;
}

template <typename T> inline void write(T x) {
    T l = 0;
    ull y = 0;
    if (!x) { putchar(48); return; }
    if (x < 0) { x = -x; putchar('-'); }
    while (x) { y = y * 10 + x % 10; x /= 10; ++l; }
    while (l) { putchar(y % 10 + 48); y /= 10; --l; }
}

template <typename T> inline void writes(T x) {
    write(x);
    putchar(' ');
}

template <typename T> inline void writeln(T x) {
    write(x);
    puts("");
}

template <typename T> inline void checkmax(T &a, T b) { a = a > b ? a : b; }

template <typename T> inline void checkmin(T &a, T b) { a = a < b ? a : b; }

const int N = 5000, MOD = 998244353;

int cm[N + 10][N + 10], ans;

inline int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; }

inline int dec(int x, int y) { x -= y; return x < 0 ? x + MOD : x; }

int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    int n, m;
    read(n);
    read(m);
    F(i, 1, N) {
        cm[i][0] = 1;
        F(j, 1, N) cm[i][j] = (LL)cm[i][j - 1] * i % MOD;
    }
    F(v, 1, m) {
        F(len, 1, n) {
            if (len + 2 <= n) ans = add(ans, (LL)dec(cm[m - v + 1][len], cm[m - v][len]) * (v - 1) % MOD * (v - 1) % MOD * cm[m][n - len - 2] % MOD * (n - len - 1) % MOD);
            if (len + 1 <= n) ans = add(ans, 2LL * dec(cm[m - v + 1][len], cm[m - v][len]) % MOD * (v - 1) % MOD * cm[m][n - len - 1] % MOD);
            if (len == n) ans = add(ans, dec(cm[m - v + 1][len], cm[m - v][len]));
        }
    }
    writeln(ans);
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
ll n;
ll b=0,c=0;
//vector<ll> B(2e5,0),C(2e5,0);
vector<bool> used(2e5,false);
vector<vector<pair<ll,ll>>> t(2e5);
//vector<ll> a(2e5,0);
ll res=0,tmp=1;
void dfs(ll x,int k){
    if(k==1){
        res=(res+(b*tmp)%mod)%mod;
        c++;
    }else{
        res=(res+(c*tmp)%mod)%mod;
        b++;
    }
    rep(i,t[x].size()){
        ll y=t[x][i].first,w=t[x][i].second%2;
        if(used[y])continue;
        used[y]=true;
        int nk=k^w;
        dfs(y,nk);
        t[x][i].second/=2;
    }
}
int main(void){
    cin>>n;
    rep(i,n-1){
        ll u,v,w;cin>>u>>v>>w;u--;v--;
        t[u].push_back({v,w});
        t[v].push_back({u,w});
    }
    rep(i,60){
        rep(j,n)used[j]=false;
        b=0,c=0;
        used[0]=true;
        dfs(0,0);
        tmp=(tmp*2)%mod;
        //cout<<res<<endl;
    }
    cout<<res<<endl;
}