#include using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; #define rep(i,n) for(int i=0;i=0;i--) #define per1(i,n) for(int i=int(n);i>0;i--) #define all(c) c.begin(),c.end() #define si(x) int(x.size()) #define pb push_back #define eb emplace_back #define fs first #define sc second template using V = vector; template using VV = vector>; template bool chmax(T& x, U y){ if(x bool chmin(T& x, U y){ if(y void mkuni(V& v){sort(all(v));v.erase(unique(all(v)),v.end());} template int lwb(const V& v, const T& a){return lower_bound(all(v),a) - v.begin();} template V Vec(size_t a) { return V(a); } template auto Vec(size_t a, Ts... ts) { return V(ts...))>(a, Vec(ts...)); } template ostream& operator<<(ostream& o,const pair &p){ return o<<"("< ostream& operator<<(ostream& o,const vector &vc){ o<<"{"; for(const T& v:vc) o< D divFloor(D a, D b){ return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0); } template D divCeil(D a, D b) { return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0); } template struct ModInt{ using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr static uint mod = mod_; uint v; ModInt():v(0){} ModInt(ll _v):v(normS(_v%mod+mod)){} explicit operator bool() const {return v!=0;} static uint normS(const uint &x){return (x [0 , mod-1] static ModInt make(const uint &x){ModInt m; m.v=x; return m;} ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));} ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));} ModInt operator-() const { return make(normS(mod-v)); } ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);} ModInt operator/(const ModInt& b) const { return *this*b.inv();} ModInt& operator+=(const ModInt& b){ return *this=*this+b;} ModInt& operator-=(const ModInt& b){ return *this=*this-b;} ModInt& operator*=(const ModInt& b){ return *this=*this*b;} ModInt& operator/=(const ModInt& b){ return *this=*this/b;} ModInt& operator++(int){ return *this=*this+1;} ModInt& operator--(int){ return *this=*this-1;} template friend ModInt operator+(T a, const ModInt& b){ return (ModInt(a) += b);} template friend ModInt operator-(T a, const ModInt& b){ return (ModInt(a) -= b);} template friend ModInt operator*(T a, const ModInt& b){ return (ModInt(a) *= b);} template friend ModInt operator/(T a, const ModInt& b){ return (ModInt(a) /= b);} ModInt pow(ll p) const { if(p<0) return inv().pow(-p); ModInt a = 1; ModInt x = *this; while(p){ if(p&1) a *= x; x *= x; p >>= 1; } return a; } ModInt inv() const { // should be prime return pow(mod-2); } // ll extgcd(ll a,ll b,ll &x,ll &y) const{ // ll p[]={a,1,0},q[]={b,0,1}; // while(*q){ // ll t=*p/ *q; // rep(i,3) swap(p[i]-=t*q[i],q[i]); // } // if(p[0]<0) rep(i,3) p[i]=-p[i]; // x=p[1],y=p[2]; // return p[0]; // } // ModInt inv() const { // ll x,y; // extgcd(v,mod,x,y); // return make(normS(x+mod)); // } bool operator==(const ModInt& b) const { return v==b.v;} bool operator!=(const ModInt& b) const { return v!=b.v;} bool operator<(const ModInt& b) const { return v>(istream &o,ModInt& x){ ll tmp; o>>tmp; x=ModInt(tmp); return o; } friend ostream& operator<<(ostream &o,const ModInt& x){ return o<>= bsf(a); do{ b >>= bsf(b); if(a>b) swap(a,b); b -= a; }while(b); return a< friend Frac operator+(T a, const Frac& b){ return (Frac(a) += b);} template friend Frac operator-(T a, const Frac& b){ return (Frac(a) -= b);} template friend Frac operator*(T a, const Frac& b){ return (Frac(a) *= b);} template friend Frac operator/(T a, const Frac& b){ return (Frac(a) /= b);} bool operator<(const Frac& r) const { return x * r.y < y * r.x; } bool operator>(const Frac& r) const { return r < *this; } bool operator<=(const Frac& r) const { return !(r < *this); } bool operator>=(const Frac& r) const { return !(*this < r); } bool operator==(const Frac& r) const { return x == r.x && y == r.y; } bool operator!=(const Frac& r) const { return !(*this == r); } Frac inv() const { return Frac(y,x); } friend ostream& operator<<(ostream &o,const Frac& x){ return o << x.x << "/" << x.y; } }; using mint = ModInt<998244353>; // using mint = Frac; V operator+(const V& a, const V& b){ V c = a; rep(i,si(b)) c[i] += b[i]; return c; } V operator*(const V& a, mint v){ V c = a; rep(i,si(a)) c[i] *= v; return c; } template struct Matrix{ int H,W; VV a; Matrix() : H(0),W(0){} Matrix(int H_,int W_) : H(H_),W(W_),a( VV(H,V(W)) ){} Matrix(const VV& v) : H(v.size()), W(v[0].size()), a(v){} static Matrix E(int n){ Matrix a(n,n); rep(i,n) a[i][i] = 1; return a; } V& operator[](int i){return a[i];} const V& operator[](int i) const {return a[i];} Matrix operator+(const Matrix& r) const { assert(H==r.H && W==r.W); VV v(H,V(W)); rep(i,H) rep(j,W) v[i][j] = a[i][j] + r.a[i][j]; return Matrix(v); } Matrix operator-(const Matrix& r) const { assert(H==r.H && W==r.W); VV v(H,V(W)); rep(i,H) rep(j,W) v[i][j] = a[i][j] - r.a[i][j]; return Matrix(v); } Matrix operator*(const Matrix& r) const { assert(W==r.H); VV v(H,V(r.W)); rep(i,H) rep(k,W) rep(j,r.W) v[i][j] += a[i][k] * r.a[k][j]; return Matrix(v); } Matrix& operator+=(const Matrix& r){return (*this)=(*this)+r;} Matrix& operator-=(const Matrix& r){return (*this)=(*this)-r;} Matrix& operator*=(const Matrix& r){return (*this)=(*this)*r;} Matrix pow(ll p) const { assert(H == W); Matrix res = E(H); Matrix x = *this; while(p){ if(p&1) res *= x; x *= x; p >>= 1; } return res; } friend ostream& operator<<(ostream &o,const Matrix& A){ rep(i,A.H){ rep(j,A.W) o< used(H); rep(j,var){ int i=0; while(i pair< int, vector > solveLinearEquation(const Matrix& A, vector b){ assert(A.H==(int)b.size()); int H = A.H, W = A.W; Matrix X(H,W+1); rep(i,H) rep(j,W) X[i][j] = A[i][j]; rep(i,H) X[i][W] = b[i]; int rank = X.sweep(W); rep(i,H){ bool allzero = true; rep(j,W) if(!iszero(X[i][j])) allzero = false; if(allzero){ if(!iszero(X[i][W])){ //0x + 0y + 0z = non0 return pair >(-1,vector()); } } } vector done(H); vector x(W); rep(j,W){ int c0 = 0, c1 = 0; int I = -1; rep(i,H){ if(iszero(X[i][j])) c0++; else if(isone(X[i][j])) c1++,I=i; } if(c0==H-1 && c1==1 && !done[I]){ x[j] = X[I][W]; done[I] = true; } } return pair >(W-rank,x); } int X,Y; V f[255][255]; V F(int x,int y){ if(0<=x&&x<=X&&0<=y&&y<=Y) return f[x][y]; return V(Y+2,0); } V Const(mint v){ V res(Y+2); res[Y+1] = v; return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; V sx(N),sy(N); rep(i,N-1){ int a,b,c; cin >> a >> b >> c; a--,b--; if(c == 1){ sx[a]++,sx[b]++; X++; }else{ sy[a]++,sy[b]++; Y++; } } int fr = N*(N-1)/2 - (N-2); rep(y,Y+1){ f[0][y] = V(Y+2); f[0][y][y] = 1; } auto A = Vec(Y+1,Y+1); V b(Y+1); rep(x,X+1) rep(y,Y+1){ mint U = x ? mint(x)/(X+2*Y) * (1 - mint(N-x-y)/fr) : 0; mint D = x != X ? (mint(X-x)/(X+2*Y)) * (mint(N-1-x-y)/fr) : 0; mint L = y ? mint(2*y)/(X+2*Y) * (1 - mint(N-x-y)/fr) : 0; mint R = y != Y ? (mint(2*(Y-y))/(X+2*Y)) * (mint(N-1-x-y)/fr) : 0; mint M = 1-U-D-L-R; show("----------------"); shows(x,y); show(U);show(D);show(L);show(R);show(M); show("-----------------"); // f(x,y) = Uf(x-1,y) + Df(x+1,y) + Lf(x,y-1) + Rf(x,y+1) + Mf(x,y) + 1/N if(x != X){ // compute f(x+1,y) f[x+1][y] = ( F(x-1,y) * U + F(x,y-1) * L + F(x,y+1) * R + F(x,y) * (M-1) + Const(mint(N).inv()) ) * (-D).inv(); }else{ if(y != Y){ // this should be 0 auto exp = F(x-1,y) * U + F(x+1,y) * D + F(x,y-1) * L + F(x,y+1) * R + F(x,y) * (M-1) + Const(mint(N).inv()); rep(i,Y+1) A[y][i] = exp[i]; b[y] = -exp[Y+1]; }else{ // f[X][Y] = const rep(i,Y+1) A[y][i] = f[X][Y][i]; b[y] = -f[X][Y][Y+1]; } } } auto p = solveLinearEquation(A,b); show(p.fs); auto Eval = [&](V f){ mint res = 0; rep(i,Y+1) res += f[i] * p.sc[i]; res += f[Y+1]; return res; }; if(false){ shows("f_debug"); rep(x,X+1){ rep(y,Y+1) cout << Eval(f[x][y]) << " "; cout << endl; } } mint bg = 0, en = 0; rep(i,N) bg += Eval(f[sx[i]][sy[i]]); en = Eval(f[X][Y]) + Eval(F(1,0)*X) + Eval(F(0,1)*Y); cout << bg-en << endl; }