#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 77, Mod = 1000000007;
int A = 1, n, sztot;
int Ev[N], Eu[N], Ex[N], Ec[N];
int S[N][2], Sz[N], Mt[N];
int a[N], b[N], c[N], d[N], PM[N];
bool M[N];
pair<int, int> F[N];
vector<int> adj[N];
vector<pair<pair<int, int>, int> > X, Y;
inline int Pow(int x, int y) {
  int r = 1;
  for (; y; y >>= 1) {
    if (y & 1) r = r * 1ll * x % Mod;
    x = x * 1ll * x % Mod;
  }
  return r;
}
inline int Calculate(vector<pair<pair<int, int>, int> > V) {
  int ans = 1;
  int sz = V.size();
  for (int i = 1; i <= sz; ++i)
    F[i] = make_pair(V[i - 1].first.first * 2 - V[i - 1].first.second, i - 1);
  sort(F + 1, F + sz + 1);
  PM[0] = 1;
  for (int i = 1; i <= sz; ++i) {
    int id = F[i].second;
    a[i] = V[id].first.first;
    b[i] = V[id].first.second;
    c[i] = V[id].second;
    d[i] = F[i].first;
    PM[i] = PM[i - 1] * 1ll * c[i] % Mod;
    if (2 * a[i] - b[i] < b[i] - 2 * a[i]) ans = ans * 1ll * (c[i] % Mod) % Mod;
  }
  for (int i = 1; i <= sz; ++i) {
    int id = lower_bound(d + 1, d + sz + 1, -d[i]) - d - 1;
    ans = ans * 1ll * Pow(PM[id], Mod - 2) % Mod;
  }
  return ans;
}
inline int Calc(vector<pair<pair<int, int>, int> > V) {
  int ans = 1, tot = 1;
  int sz = V.size();
  for (int i = 1; i <= sz; ++i) tot = tot * 1ll * V[i - 1].second % Mod;
  tot = Pow(tot, sz - 1);
  ans = tot;
  ans = ans * 1ll * Calculate(V) % Mod;
  for (int i = 0; i < (int)V.size(); ++i)
    swap(V[i].first.first, V[i].first.second);
  ans = ans * 1ll * Calculate(V) % Mod;
  return ans;
}
void dfsPre(int v, int prev = -1) {
  Sz[v] = 1;
  for (int id : adj[v]) {
    int u = Ev[id] ^ Eu[id] ^ v;
    if (u == prev || M[u]) continue;
    S[u][0] = S[v][0];
    S[u][1] = S[v][1];
    Mt[u] = Mt[v] * 1ll * Ex[id] % Mod;
    ++S[u][Ec[id]];
    dfsPre(u, v);
    Sz[v] += Sz[u];
  }
}
int GetCentroid(int v, int prev = -1) {
  for (int id : adj[v]) {
    int u = Ev[id] ^ Eu[id] ^ v;
    if (M[u] || u == prev) continue;
    if (Sz[u] * 2 > sztot) return GetCentroid(u, v);
  }
  return v;
}
void dfsGet(int v, int prev = -1) {
  X.push_back(make_pair(make_pair(S[v][0], S[v][1]), Mt[v]));
  for (int id : adj[v]) {
    int u = Ev[id] ^ Eu[id] ^ v;
    if (M[u] || u == prev) continue;
    dfsGet(u, v);
  }
}
void Solve(int v) {
  dfsPre(v);
  sztot = Sz[v];
  int root = GetCentroid(v);
  Mt[root] = 1;
  S[root][0] = S[root][1] = 0;
  dfsPre(root);
  M[root] = 1;
  vector<int> Ch;
  for (int id : adj[root]) {
    int u = Ev[id] ^ Eu[id] ^ root;
    if (!M[u]) Ch.push_back(u);
  }
  for (int u : Ch) {
    dfsGet(u);
    A = A * 1ll * Pow(Calc(X), Mod - 2) % Mod;
    for (auto x : X) Y.push_back(x);
    X.clear();
  }
  Y.push_back(make_pair(make_pair(S[root][0], S[root][1]), Mt[root]));
  A = A * 1ll * Calc(Y) % Mod;
  Y.clear();
  for (int u : Ch) Solve(u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i)
    scanf("%d %d %d %d", Ev + i, Eu + i, Ex + i, Ec + i),
        adj[Ev[i]].push_back(i), adj[Eu[i]].push_back(i);
  Solve(1);
  printf("%d\n", A);
  return 0;
}
