#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(1e5)+7;
const int mod = 998244353;
int add(int x , int y){
x += y;
if (x >= mod) x -= mod;
return x;
}
void self_add(int &x , int y){
x = add(x , y);
}
int sub(int x , int y){
x -= y;
if (x < 0) x += mod;
return x;
}
void self_sub(int &x , int y){
x = sub(x , y);
}
int mul(int x , int y){
return (1ll * x * y) % mod;
}
int sqr(int x){
return mul(x , x);
}
int Pow(int a , int n){
int ans = 1;
for (; n > 0 ; n /= 2 , a = mul(a , a)){
if (n&1) ans = mul(ans , a);
}
return ans;
}
int n;
vector<ii> g[maxN];
int sz[maxN];
bool del[maxN];
void dfs_size(int u , int par){
sz[u] = 1;
for (ii e : g[u]){
int v = e.fi;
if (v != par && del[v] == 0){
dfs_size(v , u);
sz[u] += sz[v];
}
}
}
int dfs_find(int u , int par , int half){
for (ii e : g[u]){
int v = e.fi;
if (v != par && del[v] == 0 && sz[v] > half){
return dfs_find(v , u , half);
}
}
return u;
}
vector<pair<ii , int>> p;
void dfs_prepare(int u , int par , int c , int d){
if (par != 0){
p.push_back({{c , d} , u});
}
for (ii e : g[u]){
int v = e.fi;
int w = e.se;
if (v != par && del[v] == 0){
dfs_prepare(v , u , min(c , w) , d + 1);
}
}
}
int f[3] , res[maxN];
void prepare(int u , int par , int c , int d){
dfs_prepare(u , par , c , d);
if (par == 0){
for (auto it : p){
int v = it.se;
int C = it.fi.fi;
int D = it.fi.se;
self_add(res[u] , mul(C , sqr(D)));
self_add(res[v] , mul(C , sqr(D)));
}
}
sort(all(p));
f[0] = f[1] = f[2] = 0;
for (auto it : p){
int v = it.se;
int C = it.fi.fi;
int D = it.fi.se;
if (par == 0){
self_add(res[v] , mul(f[0] , sqr(D)));
self_add(res[v] , mul(f[1] , D));
self_add(res[v] , f[2]);
}
else{
self_sub(res[v] , mul(f[0] , sqr(D)));
self_sub(res[v] , mul(f[1] , D));
self_sub(res[v] , f[2]);
}
self_add(f[0] , C);
self_add(f[1] , mul(2 , mul(C , D)));
self_add(f[2] , mul(C , sqr(D)));
}
reverse(all(p));
f[0] = f[1] = f[2] = 0;
for (auto it : p){
int v = it.se;
int C = it.fi.fi;
int D = it.fi.se;
if (par == 0){
self_add(res[v] , mul(f[0] , mul(C , sqr(D))));
self_add(res[v] , mul(f[1] , mul(C , D)));
self_add(res[v] , mul(f[2] , C));
}
else{
self_sub(res[v] , mul(f[0] , mul(C , sqr(D))));
self_sub(res[v] , mul(f[1] , mul(C , D)));
self_sub(res[v] , mul(f[2] , C));
}
self_add(f[0] , 1);
self_add(f[1] , mul(2 , D));
self_add(f[2] , sqr(D));
}
p.clear();
}
void dfs_solve(int u){
dfs_size(u , 0);
u = dfs_find(u , 0 , sz[u] / 2);
del[u] = 1;
prepare(u , 0 , INT_MAX , 0);
for (ii e : g[u]){
int v = e.fi;
int w = e.se;
if (del[v] == 0) prepare(v , u , w , 1);
}
for (ii e : g[u]){
int v = e.fi;
if (del[v] == 0){
dfs_solve(v);
}
}
}
void solve(){
cin >> n;
for (int i = 1 ; i < n ; i++){
int u , v , w;
cin >> u >> v >> w;
g[u].push_back({v , w});
g[v].push_back({u , w});
}
dfs_solve(1);
for (int i = 1 ; i <= n ; i++) cout << res[i] << "\n";
}
#define name "netw"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUo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wgaW50IHBhcil7CiAgICBzelt1XSA9IDE7CiAgICBmb3IgKGlpIGUgOiBnW3VdKXsKICAgICAgICBpbnQgdiA9IGUuZmk7CiAgICAgICAgaWYgKHYgIT0gcGFyICYmIGRlbFt2XSA9PSAwKXsKICAgICAgICAgICAgZGZzX3NpemUodiAsIHUpOwogICAgICAgICAgICBzelt1XSArPSBzelt2XTsKICAgICAgICB9CiAgICB9Cn0KCmludCBkZnNfZmluZChpbnQgdSAsIGludCBwYXIgLCBpbnQgaGFsZil7CiAgICBmb3IgKGlpIGUgOiBnW3VdKXsKICAgICAgICBpbnQgdiA9IGUuZmk7CiAgICAgICAgaWYgKHYgIT0gcGFyICYmIGRlbFt2XSA9PSAwICYmIHN6W3ZdID4gaGFsZil7CiAgICAgICAgICAgIHJldHVybiBkZnNfZmluZCh2ICwgdSAsIGhhbGYpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiB1Owp9Cgp2ZWN0b3I8cGFpcjxpaSAsIGludD4+IHA7Cgp2b2lkIGRmc19wcmVwYXJlKGludCB1ICwgaW50IHBhciAsIGludCBjICwgaW50IGQpewogICAgaWYgKHBhciAhPSAwKXsKICAgICAgICBwLnB1c2hfYmFjayh7e2MgLCBkfSAsIHV9KTsKICAgIH0KICAgIGZvciAoaWkgZSA6IGdbdV0pewogICAgICAgIGludCB2ID0gZS5maTsKICAgICAgICBpbnQgdyA9IGUuc2U7CiAgICAgICAgaWYgKHYgIT0gcGFyICYmIGRlbFt2XSA9PSAwKXsKICAgICAgICAgICAgZGZzX3ByZXBhcmUodiAsIHUgLCBtaW4oYyAsIHcpICwgZCArIDEpOwogICAgICAgIH0KICAgIH0KfQoKaW50IGZbM10gLCByZXNbbWF4Tl07Cgp2b2lkIHByZXBhcmUoaW50IHUgLCBpbnQgcGFyICwgaW50IGMgLCBpbnQgZCl7CiAgICBkZnNfcHJlcGFyZSh1ICwgcGFyICwgYyAsIGQpOwogICAgaWYgKHBhciA9PSAwKXsKICAgICAgICBmb3IgKGF1dG8gaXQgOiBwKXsKICAgICAgICAgICAgaW50IHYgPSBpdC5zZTsKICAgICAgICAgICAgaW50IEMgPSBpdC5maS5maTsKICAgICAgICAgICAgaW50IEQgPSBpdC5maS5zZTsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3VdICwgbXVsKEMgLCBzcXIoRCkpKTsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3ZdICwgbXVsKEMgLCBzcXIoRCkpKTsKICAgICAgICB9CiAgICB9CiAgICBzb3J0KGFsbChwKSk7CiAgICBmWzBdID0gZlsxXSA9IGZbMl0gPSAwOwogICAgZm9yIChhdXRvIGl0IDogcCl7CiAgICAgICAgaW50IHYgPSBpdC5zZTsKICAgICAgICBpbnQgQyA9IGl0LmZpLmZpOwogICAgICAgIGludCBEID0gaXQuZmkuc2U7CiAgICAgICAgaWYgKHBhciA9PSAwKXsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3ZdICwgbXVsKGZbMF0gLCBzcXIoRCkpKTsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3ZdICwgbXVsKGZbMV0gLCBEKSk7CiAgICAgICAgICAgIHNlbGZfYWRkKHJlc1t2XSAsIGZbMl0pOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBzZWxmX3N1YihyZXNbdl0gLCBtdWwoZlswXSAsIHNxcihEKSkpOwogICAgICAgICAgICBzZWxmX3N1YihyZXNbdl0gLCBtdWwoZlsxXSAsIEQpKTsKICAgICAgICAgICAgc2VsZl9zdWIocmVzW3ZdICwgZlsyXSk7CiAgICAgICAgfQogICAgICAgIHNlbGZfYWRkKGZbMF0gLCBDKTsKICAgICAgICBzZWxmX2FkZChmWzFdICwgbXVsKDIgLCBtdWwoQyAsIEQpKSk7CiAgICAgICAgc2VsZl9hZGQoZlsyXSAsIG11bChDICwgc3FyKEQpKSk7CiAgICB9CiAgICByZXZlcnNlKGFsbChwKSk7CiAgICBmWzBdID0gZlsxXSA9IGZbMl0gPSAwOwogICAgZm9yIChhdXRvIGl0IDogcCl7CiAgICAgICAgaW50IHYgPSBpdC5zZTsKICAgICAgICBpbnQgQyA9IGl0LmZpLmZpOwogICAgICAgIGludCBEID0gaXQuZmkuc2U7CiAgICAgICAgaWYgKHBhciA9PSAwKXsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3ZdICwgbXVsKGZbMF0gLCBtdWwoQyAsIHNxcihEKSkpKTsKICAgICAgICAgICAgc2VsZl9hZGQocmVzW3ZdICwgbXVsKGZbMV0gLCBtdWwoQyAsIEQpKSk7CiAgICAgICAgICAgIHNlbGZfYWRkKHJlc1t2XSAsIG11bChmWzJdICwgQykpOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBzZWxmX3N1YihyZXNbdl0gLCBtdWwoZlswXSAsIG11bChDICwgc3FyKEQpKSkpOwogICAgICAgICAgICBzZWxmX3N1YihyZXNbdl0gLCBtdWwoZlsxXSAsIG11bChDICwgRCkpKTsKICAgICAgICAgICAgc2VsZl9zdWIocmVzW3ZdICwgbXVsKGZbMl0gLCBDKSk7CiAgICAgICAgfQogICAgICAgIHNlbGZfYWRkKGZbMF0gLCAxKTsKICAgICAgICBzZWxmX2FkZChmWzFdICwgbXVsKDIgLCBEKSk7CiAgICAgICAgc2VsZl9hZGQoZlsyXSAsIHNxcihEKSk7CiAgICB9CiAgICBwLmNsZWFyKCk7Cn0KCnZvaWQgZGZzX3NvbHZlKGludCB1KXsKICAgIGRmc19zaXplKHUgLCAwKTsKICAgIHUgPSBkZnNfZmluZCh1ICwgMCAsIHN6W3VdIC8gMik7CiAgICBkZWxbdV0gPSAxOwogICAgcHJlcGFyZSh1ICwgMCAsIElOVF9NQVggLCAwKTsKICAgIGZvciAoaWkgZSA6IGdbdV0pewogICAgICAgIGludCB2ID0gZS5maTsKICAgICAgICBpbnQgdyA9IGUuc2U7CiAgICAgICAgaWYgKGRlbFt2XSA9PSAwKSBwcmVwYXJlKHYgLCB1ICwgdyAsIDEpOwogICAgfQog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