#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

struct edge{ ll to, cost; };

#define MAX_N 200005
int nn = MAX_N; // 複数回根付き木を作成する場合は適宜変える.
vector<edge> pre_G[MAX_N];
vector<edge> G[MAX_N];
int par_[MAX_N];
ll par_cost[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r){
    rep(i,nn){
        G[i].clear();
        par_[i] = 0;
        flag_netsukigi[i] = 0;
    }
    queue<int> que;
    que.push(r);
    flag_netsukigi[r] = 1;
    par_[r] = -1;

    while(que.size() > 0){
        int x = que.front();
        que.pop();
        flag_netsukigi[x] = 1;
        rep(i,pre_G[x].size()){
            if(flag_netsukigi[pre_G[x][i].to] == 0){
                par_[pre_G[x][i].to] = x;
                par_cost[pre_G[x][i].to] = pre_G[x][i].cost;
                G[x].push_back(pre_G[x][i]);
                que.push(pre_G[x][i].to);
            }
        }
    }
}

ll dp[MAX_N][64][2];
ll ans;
const ll MOD = 1000000007;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    nn = n;

    rep(i,n-1){
        ll from, to, cost;
        cin >> from >> to >> cost;
        from--; to--; // 0-indexにするため.
        edge e1, e2;
        e1.to = to; e2.to = from;
        e1.cost = cost; e2.cost = cost;
        pre_G[from].push_back(e1);
        pre_G[to].push_back(e2);
    }

    make_netsukigi(0);

    int sons[n] = {};
    queue<int> que;
    rep(i,n){
        sons[i] = G[i].size();
        if(G[i].size() == 0){
            que.push(i);
        }
        rep(j,60) dp[i][j][0] = 1;
    }

    while(que.size()){
        int x = que.front();
        que.pop();
        if(x == 0) continue;
        int px = par_[x];
        ll cost = par_cost[x];
        bitset<64> bi(cost);
        rep(i,60) if(bi[i]) swap(dp[x][i][0],dp[x][i][1]);
           
        rep(i,60){
            ans += (1LL<<i) % MOD * dp[px][i][0] % MOD * dp[x][i][1] % MOD;
            ans += (1LL<<i) % MOD * dp[px][i][1] % MOD * dp[x][i][0] % MOD;
            ans %= MOD;
        }
        rep(i,60){
            rep(j,2){
                dp[px][i][j] += dp[x][i][j];
            }
        }

        sons[px]--;
        if(sons[px]==0) que.push(px);
    }

    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

int main() {
    int N;
    cin >> N;
    V<int> A(N), B(N);
    int sz = N * 2;
    V<int> tp(sz), com(sz, -1);

    bool ng = false;

    rep(i, N) {
        cin >> A[i] >> B[i];
        if (A[i] != -1) {
            --A[i];
            if (tp[A[i]]) ng = true;
            tp[A[i]] = i + 1;
        }
        if (B[i] != -1) {
            --B[i];
            if (tp[B[i]]) ng = true;
            tp[B[i]] = -(i + 1);
        }

        if (A[i] != -1 && B[i] != -1) {
            com[A[i]] = B[i];
            com[B[i]] = A[i];
        }
    }
    if (ng) {
        puts("No");
        return 0;
    }

    V<bool> dp(sz + 1);
    dp[0] = true;

    rep(i, sz) {
        if (!dp[i]) continue;
        for (int j = i + 1; j < sz; ++j) {
            int w = j - i + 1;
            if (w & 1) continue;
            w /= 2;
            bool ok = true;
            V<bool> exist(N);

            rep(k, w) {
                int p = i + k, q = i + k + w;
                if (com[p] != -1 && !(i <= com[p] && com[p] <= j)) {
                    ok = false;
                }
                if (com[q] != -1 && !(i <= com[q] && com[q] <= j)) {
                    ok = false;
                }

                bool same = true;
                if (tp[p] != 0 && tp[q] != 0) {
                    if (tp[p] < 0 || tp[p] + tp[q] != 0) {
                        ok = false;
                    } else
                        same = true;
                }
                if (tp[p] < 0 || tp[q] > 0) {
                    ok = false;
                } else {
                    if (tp[p] != 0) {
                        int v = tp[p] - 1;
                        if (exist[v]) {
                            ok = false;
                        }
                        exist[v] = true;
                    }
                    if (!same && tp[q] != 0) {
                        int v = -tp[q] - 1;
                        if (exist[v]) {
                            ok = false;
                        }
                        exist[v] = true;
                    }
                }
            }
            if (ok) {
                dp[j + 1] = true;
            }
        }
    }
    puts(dp[sz] ? "Yes" : "No");

    return 0;
}
