#include <bits/stdc++.h>
using namespace std;
typedef int64_t i64;
typedef long long ll;
typedef long double ld;
typedef unsigned long long Ull;

#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(int &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void read(ll &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void readarr(ll a[], ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readarr(int a[], int n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<ll> &a, ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<int> &a, int n) {for (int i = 0; i < n; i++)read(a[i]);}
void aout(ll a[], ll n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
void aout(int a[], int n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
const int MAXN = (int)1e6 + 7;
const int MOD = 998244353;
const int INF = (int)2e9 + 7;
const ll LINF = (i64)1e18;
const ld PI = 3.1415926535897932384626433832795;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
	int n;
	read(n);
	vector<pair<ll, ll>> p(n), cho;
	ll tot = 0;
	for (int i = 0; i < n; i++) {
		read(p[i].first), read(p[i].second);
	}
	sort(rall(p));
	for (int i = 0; i < n; i++) {
		cho.pb({p[i].second + 2 * p[i].first, p[i].first});
		tot += p[i].first;
	}
	sort(rall(cho));
	ll now = 0, ans = 0;
	for (int i = 0; i < cho.size(); i++) {
		if (tot < now)break;
		ans++;
		now += cho[i].first - cho[i].second;
		tot -= cho[i].second;
	}
	cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t = 1;
	//read(t);
	while (t--)
	{
		solve();
	}
}

// かります！ごめんなさい!

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/





typedef long long ll;
int N;
vector<pair<int, ll>> E[201010];
//---------------------------------------------------------------------------------------------------
ll xo[201010];
void dfs(int cu, int pa = -1) {
    fore(p, E[cu]) if (p.first != pa) {
            xo[p.first] = xo[cu] ^ p.second;
            dfs(p.first, cu);
        }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N - 1) {
        int a, b; ll c; cin >> a >> b >> c;
        a--; b--;
        E[a].push_back({ b, c });
        E[b].push_back({ a, c });
    }

    dfs(0);

    mint ans = 0;
    rep(d, 0, 61) {
        mint a = 1;
        ll mask = 1;
        rep(i, 0, d) a *= 2, mask *= 2;

        mint cnt0 = 0, cnt1 = 0;
        rep(i, 0, N) {
            if (xo[i] & mask) cnt1 += 1;
            else cnt0 += 1;
        }

        mint cnt = 0;
        rep(i, 0, N) {
            if (xo[i] & mask) cnt += cnt0;
            else cnt += cnt1;
        }
        cnt /= 2;
        ans += a * cnt;
    }
    cout << ans << endl;
}