/** * date : 2021-11-23 23:04:51 */ #define NDEBUG using namespace std; // intrinstic #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // utility namespace Nyaan { using ll = long long; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; template using V = vector; template using VV = vector>; using vi = vector; using vl = vector; using vd = V; using vs = V; using vvi = vector>; using vvl = vector>; template struct P : pair { template P(Args... args) : pair(args...) {} using pair::first; using pair::second; T &x() { return first; } const T &x() const { return first; } U &y() { return second; } const U &y() const { return second; } P &operator+=(const P &r) { first += r.first; second += r.second; return *this; } P &operator-=(const P &r) { first -= r.first; second -= r.second; return *this; } P &operator*=(const P &r) { first *= r.first; second *= r.second; return *this; } P operator+(const P &r) const { return P(*this) += r; } P operator-(const P &r) const { return P(*this) -= r; } P operator*(const P &r) const { return P(*this) *= r; } }; using pl = P; using pi = P; using vp = V; constexpr int inf = 1001001001; constexpr long long infLL = 4004004004004004004LL; template int sz(const T &t) { return t.size(); } template inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template inline T Max(const vector &v) { return *max_element(begin(v), end(v)); } template inline T Min(const vector &v) { return *min_element(begin(v), end(v)); } template inline long long Sum(const vector &v) { return accumulate(begin(v), end(v), 0LL); } template int lb(const vector &v, const T &a) { return lower_bound(begin(v), end(v), a) - begin(v); } template int ub(const vector &v, const T &a) { return upper_bound(begin(v), end(v), a) - begin(v); } constexpr long long TEN(int n) { long long ret = 1, x = 10; for (; n; x *= x, n >>= 1) ret *= (n & 1 ? x : 1); return ret; } template pair mkp(const T &t, const U &u) { return make_pair(t, u); } template vector mkrui(const vector &v, bool rev = false) { vector ret(v.size() + 1); if (rev) { for (int i = int(v.size()) - 1; i >= 0; i--) ret[i] = v[i] + ret[i + 1]; } else { for (int i = 0; i < int(v.size()); i++) ret[i + 1] = ret[i] + v[i]; } return ret; }; template vector mkuni(const vector &v) { vector ret(v); sort(ret.begin(), ret.end()); ret.erase(unique(ret.begin(), ret.end()), ret.end()); return ret; } template vector mkord(int N, F f) { vector ord(N); iota(begin(ord), end(ord), 0); sort(begin(ord), end(ord), f); return ord; } template vector mkinv(vector &v) { int max_val = *max_element(begin(v), end(v)); vector inv(max_val + 1, -1); for (int i = 0; i < (int)v.size(); i++) inv[v[i]] = i; return inv; } } // namespace Nyaan // bit operation namespace Nyaan { __attribute__((target("popcnt"))) inline int popcnt(const u64 &a) { return _mm_popcnt_u64(a); } inline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; } inline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; } inline int msb(const u64 &a) { return a ? 63 - __builtin_clzll(a) : -1; } template inline int gbit(const T &a, int i) { return (a >> i) & 1; } template inline void sbit(T &a, int i, bool b) { if (gbit(a, i) != b) a ^= T(1) << i; } constexpr long long PW(int n) { return 1LL << n; } constexpr long long MSK(int n) { return (1LL << n) - 1; } } // namespace Nyaan // inout namespace Nyaan { 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) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &... u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &... u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &... u) { cout << t; outr(u...); } struct IoSetupNya { IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetupnya; } // namespace Nyaan // debug namespace DebugImpl { template struct is_specialize : false_type {}; template struct is_specialize< U, typename conditional::type> : true_type {}; template struct is_specialize< U, typename conditional::type> : true_type {}; template struct is_specialize::value, void>> : true_type { }; void dump(const char& t) { cerr << t; } void dump(const string& t) { cerr << t; } void dump(const bool& t) { cerr << (t ? "true" : "false"); } template ::value, nullptr_t> = nullptr> void dump(const U& t) { cerr << t; } template void dump(const T& t, enable_if_t::value>* = nullptr) { string res; if (t == Nyaan::inf) res = "inf"; if constexpr (is_signed::value) { if (t == -Nyaan::inf) res = "-inf"; } if constexpr (sizeof(T) == 8) { if (t == Nyaan::infLL) res = "inf"; if constexpr (is_signed::value) { if (t == -Nyaan::infLL) res = "-inf"; } } if (res.empty()) res = to_string(t); cerr << res; } template void dump(const pair&); template void dump(const pair&); template void dump(const T& t, enable_if_t::value>* = nullptr) { cerr << "[ "; for (auto it = t.begin(); it != t.end();) { dump(*it); cerr << (++it == t.end() ? "" : ", "); } cerr << " ]"; } template void dump(const pair& t) { cerr << "( "; dump(t.first); cerr << ", "; dump(t.second); cerr << " )"; } template void dump(const pair& t) { cerr << "[ "; for (int i = 0; i < t.second; i++) { dump(t.first[i]); cerr << (i == t.second - 1 ? "" : ", "); } cerr << " ]"; } void trace() { cerr << endl; } template void trace(Head&& head, Tail&&... tail) { cerr << " "; dump(head); if (sizeof...(tail) != 0) cerr << ","; trace(forward(tail)...); } } // namespace DebugImpl #ifdef NyaanDebug #define trc(...) \ do { \ cerr << "## " << #__VA_ARGS__ << " = "; \ DebugImpl::trace(__VA_ARGS__); \ } while (0) #else #define trc(...) (void(0)) #endif // macro #define each(x, v) for (auto&& x : v) #define each2(x, y, v) for (auto&& [x, y] : v) #define all(v) (v).begin(), (v).end() #define rep(i, N) for (long long i = 0; i < (long long)(N); i++) #define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--) #define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++) #define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--) #define reg(i, a, b) for (long long i = (a); i < (b); i++) #define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--) #define fi first #define se second #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) #define in2(s, t) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i]); \ } #define in3(s, t, u) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i], u[i]); \ } #define in4(s, t, u, v) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i], u[i], v[i]); \ } #define die(...) \ do { \ Nyaan::out(__VA_ARGS__); \ return; \ } while (0) namespace Nyaan { void solve(); } int main() { Nyaan::solve(); } // template struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template using Edges = vector>; template using WeightedGraph = vector>; using UnweightedGraph = vector>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template WeightedGraph wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template Edges esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) { Edges es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template vector> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector> d(N, vector(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } // 一般のグラフのstからの距離!!!! // unvisited nodes : d = -1 vector Depth(const UnweightedGraph &g, int start = 0) { int n = g.size(); vector ds(n, -1); ds[start] = 0; queue q; q.push(start); while (!q.empty()) { int c = q.front(); q.pop(); int dc = ds[c]; for (auto &d : g[c]) { if (ds[d] == -1) { ds[d] = dc + 1; q.push(d); } } } return ds; } // Depth of Rooted Weighted Tree // unvisited nodes : d = -1 template vector Depth(const WeightedGraph &g, int start = 0) { vector d(g.size(), -1); auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void { d[cur] = val; for (auto &dst : g[cur]) { if (dst == par) continue; rec(rec, dst, val + dst.cost, cur); } }; dfs(dfs, start, 0); return d; } // Diameter of Tree // return value : { {u, v}, length } pair, int> Diameter(const UnweightedGraph &g) { auto d = Depth(g, 0); int u = max_element(begin(d), end(d)) - begin(d); d = Depth(g, u); int v = max_element(begin(d), end(d)) - begin(d); return make_pair(make_pair(u, v), d[v]); } // Diameter of Weighted Tree // return value : { {u, v}, length } template pair, T> Diameter(const WeightedGraph &g) { auto d = Depth(g, 0); int u = max_element(begin(d), end(d)) - begin(d); d = Depth(g, u); int v = max_element(begin(d), end(d)) - begin(d); return make_pair(make_pair(u, v), d[v]); } // nodes on the path u-v ( O(N) ) template vector Path(G &g, int u, int v) { vector ret; int end = 0; auto dfs = [&](auto rec, int cur, int par = -1) -> void { ret.push_back(cur); if (cur == v) { end = 1; return; } for (int dst : g[cur]) { if (dst == par) continue; rec(rec, dst, cur); if (end) return; } if (end) return; ret.pop_back(); }; dfs(dfs, u); return ret; } ostream& operator<<(ostream& os, __int128_t x) { if (x == 0) return os << 0; if (x < 0) os << '-', x = -x; string res; while (x) res.push_back(x % 10 + '0'), x /= 10; reverse(begin(res), end(res)); return os << res; } struct Rational { using R = Rational; using i128 = __int128_t; // using i64 = long long; // using u64 = unsigned long long; using i64 = __int128_t; using u64 = __uint128_t; i64 x, y; Rational() : x(0), y(1) {} Rational(i64 _x, i64 _y = 1) : x(_x), y(_y) { assert(y != 0); if (_y != 1) { i64 g = gcd(x, y); if (g != 0) x /= g, y /= g; if (y < 0) x = -x, y = -y; } } u64 gcd(i64 A, i64 B) { u64 a = A >= 0 ? A : -A; u64 b = B >= 0 ? B : -B; return __gcd(a, b); /* if (a == 0 || b == 0) return a + b; int n = __builtin_ctzll(a); int m = __builtin_ctzll(b); a >>= n; b >>= m; while (a != b) { int d = __builtin_ctzll(a - b); bool f = a > b; u64 c = f ? a : b; b = f ? b : a; a = (c - b) >> d; } return a << min(n, m); */ } friend R operator+(const R& l, const R& r) { return R(l.x * r.y + l.y * r.x, l.y * r.y); } friend R operator-(const R& l, const R& r) { return R(l.x * r.y - l.y * r.x, l.y * r.y); } friend R operator*(const R& l, const R& r) { return R(l.x * r.x, l.y * r.y); } friend R operator/(const R& l, const R& r) { assert(r.x != 0); return R(l.x * r.y, l.y * r.x); } R& operator+=(const R& r) { return (*this) = (*this) + r; } R& operator-=(const R& r) { return (*this) = (*this) - r; } R& operator*=(const R& r) { return (*this) = (*this) * r; } R& operator/=(const R& r) { return (*this) = (*this) / r; } R operator-() const { R r; r.x = -x, r.y = y; return r; } R inverse() const { assert(x != 0); R r; r.x = y, r.y = x; if (x < 0) r.x = -r.x, r.y = -r.y; return r; } R pow(long long p) const { R res(1), base(*this); while (p) { if (p & 1) res *= base; base *= base; p >>= 1; } return res; } friend bool operator==(const R& l, const R& r) { return l.x == r.x && l.y == r.y; }; friend bool operator!=(const R& l, const R& r) { return l.x != r.x || l.y != r.y; }; friend bool operator<(const R& l, const R& r) { return i128(l.x) * r.y < i128(l.y) * r.x; }; friend bool operator<=(const R& l, const R& r) { return l < r || l == r; } friend bool operator>(const R& l, const R& r) { return i128(l.x) * r.y > i128(l.y) * r.x; }; friend bool operator>=(const R& l, const R& r) { return l > r || l == r; } friend ostream& operator<<(ostream& os, const R& r) { os << r.x; if (r.x != 0 && r.y != 1) os << "/" << r.y; return os; } long long toMint(long long mod) { assert(mod != 0); i64 a = y, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return i128((u % mod + mod) % mod) * x % mod; } }; template struct Binomial { vector fc; Binomial(int = 0) { fc.emplace_back(1); } void extend() { int n = fc.size(); R nxt = fc.back() * n; fc.push_back(nxt); } R fac(int n) { while ((int)fc.size() <= n) extend(); return fc[n]; } R finv(int n) { return fac(n).inverse(); } R inv(int n) { return R{1, max(n, 1)}; } R C(int n, int r) { if (n < 0 or r < 0 or n < r) return R{0}; return fac(n) * finv(n - r) * finv(r); } R operator()(int n, int r) { return C(n, r); } }; template std::pair GaussElimination(vector> &a, int pivot_end = -1, bool diagonalize = false) { int H = a.size(), W = a[0].size(); int rank = 0, je = pivot_end; if (je == -1) je = W; mint det = 1; for (int j = 0; j < je; j++) { int idx = -1; for (int i = rank; i < H; i++) { if (a[i][j] != mint(0)) { idx = i; break; } } if (idx == -1) { det = 0; continue; } if (rank != idx) { det = -det; swap(a[rank], a[idx]); } det *= a[rank][j]; if (diagonalize && a[rank][j] != mint(1)) { mint coeff = a[rank][j].inverse(); for (int k = j; k < W; k++) a[rank][k] *= coeff; } int is = diagonalize ? 0 : rank + 1; for (int i = is; i < H; i++) { if (i == rank) continue; if (a[i][j] != mint(0)) { mint coeff = a[i][j] / a[rank][j]; for (int k = j; k < W; k++) a[i][k] -= a[rank][k] * coeff; } } rank++; } return make_pair(rank, det); } template vector> LinearEquation(vector> a, vector b) { int H = a.size(), W = a[0].size(); for (int i = 0; i < H; i++) a[i].push_back(b[i]); auto p = GaussElimination(a, W, true); int rank = p.first; for (int i = rank; i < H; ++i) { if (a[i][W] != 0) return vector>{}; } vector> res(1, vector(W)); vector pivot(W, -1); for (int i = 0, j = 0; i < rank; ++i) { while (a[i][j] == 0) ++j; res[0][j] = a[i][W], pivot[j] = i; } for (int j = 0; j < W; ++j) { if (pivot[j] == -1) { vector x(W); x[j] = 1; for (int k = 0; k < j; ++k) { if (pivot[k] != -1) x[k] = -a[pivot[k]][j]; } res.push_back(x); } } return res; } using mint = Rational; using namespace Nyaan; void Nyaan::solve() { inl(N); map ws; Edges es; rep(i, N - 1) { inl(u, v, w); --u, --v; es.emplace_back(u, v, w); ws[w]++; } int X = 0, Y = 0; mint a = 0, b = 0; tie(a, X) = *begin(ws); if (sz(ws) == 2) tie(b, Y) = *next(begin(ws)); vector> A((X + 1) * (Y + 1) - 1, vector((X + 1) * (Y + 1), mint{})); vector B((X + 1) * (Y + 1)); auto id = [&](int i, int j) { return i * (Y + 1) + j; }; rep(i, X + 1) rep(j, Y + 1) { if (i == X and j == Y) continue; mint p = N * (N - 1) / 2 - (X + Y - 1); mint q = N - i - j; if (i != 0) A[id(i, j)][id(i - 0, j)] += a * i * q; if (i != 0) A[id(i, j)][id(i - 1, j)] += a * i * (p - q); if (j != 0) A[id(i, j)][id(i, j - 0)] += b * j * q; if (j != 0) A[id(i, j)][id(i, j - 1)] += b * j * (p - q); if (i != X) A[id(i, j)][id(i + 0, j)] += a * (X - i) * (p - q + 1); if (i != X) A[id(i, j)][id(i + 1, j)] += a * (X - i) * (q - 1); if (j != Y) A[id(i, j)][id(i, j + 0)] += b * (Y - j) * (p - q + 1); if (j != Y) A[id(i, j)][id(i, j + 1)] += b * (Y - j) * (q - 1); mint all = (a * X + b * Y) * p; A[id(i, j)][id(i, j)] -= all; B[id(i, j)] = -all / N; } auto xs = LinearEquation(A, B)[0]; trc(LinearEquation(A, B)); rep(i, X + 1) rep(j, Y + 1) { trc(i, j, xs[id(i, j)]); } vi cx(N), cy(N); each(e, es) { (a == e.cost ? cx : cy)[e.src]++; (a == e.cost ? cx : cy)[e.to]++; } trc(cx, cy); mint ans = 0; rep(i, N) ans += xs[id(cx[i], cy[i])]; ans -= xs[id(1, 0)] * X + xs[id(0, 1)] * Y + xs[id(X, Y)]; //for(auto&r:xs)out(1.0*r.x/r.y); //out(ans); out(1.0 * ans.x / ans.y); }