#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 2, maxn2 = 4e5 + 10, mod = 1e9 + 7;
const long long inf = 1e18;
int x[maxn], c[maxn], SZ[maxn], ans = 1;
bool mark[maxn];
vector<pair<int, int> > v[maxn];
struct Fenwick {
  int zr[maxn2], cnt[maxn2], tot1, tot2;
  Fenwick() {
    fill(zr, zr + maxn2, 1);
    memset(cnt, 0, sizeof cnt);
    tot1 = 1;
    tot2 = 0;
  }
  vector<pair<int, pair<int, int> > > vec;
  void add(int id, int x) {
    tot1 = 1ll * tot1 * x % mod;
    tot2++;
    while (id < maxn2) {
      vec.push_back({id, {zr[id], cnt[id]}});
      cnt[id]++;
      zr[id] = 1ll * zr[id] * x % mod;
      id += (id & -id);
    }
  }
  pair<int, int> ask(int id) {
    int A = 1, B = 0;
    while (id > 0) {
      B += cnt[id];
      A = 1ll * A * zr[id] % mod;
      id -= (id & -id);
    }
    return {A, B};
  }
  inline void Restart() {
    for (int i = int((vec).size()) - 1; i >= 0; i--) {
      auto it = vec[i];
      zr[it.first] = it.second.first;
      cnt[it.first] = it.second.second;
    }
    vec.clear();
    tot1 = 1;
    tot2 = 0;
  }
};
Fenwick fn1, fn2;
int Pow(int a, int b) {
  b = b % (mod - 1);
  if (b < 0) b += mod - 1;
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1ll * ans * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ans;
}
void dfsz(int u, int par = -1) {
  SZ[u] = 1;
  for (pair<int, int> p : v[u]) {
    if (p.first != par && !mark[p.first])
      dfsz(p.first, u), SZ[u] += SZ[p.first];
  }
}
int dfsc(int u, int N, int par = -1) {
  for (pair<int, int> p : v[u]) {
    if (p.first != par && !mark[p.first] && SZ[p.first] > N)
      return dfsc(p.first, N, u);
  }
  return u;
}
inline void Add(int X, int C1, int C2) {
  fn1.add(C2 - 2 * C1 + maxn + maxn, X);
  fn2.add(C1 - 2 * C2 + maxn + maxn, X);
}
inline void Get(int X, int C1, int C2) {
  pair<int, int> p1 = fn1.ask(2 * C1 - C2 + maxn + maxn),
                 p2 = fn2.ask(2 * C2 - C1 + maxn + maxn);
  p2.first = 1ll * fn2.tot1 * Pow(p2.first, mod - 2) % mod;
  p2.second = fn2.tot2 - p2.second;
  ans = 1ll * ans * p1.first % mod * Pow(p2.first, mod - 2) % mod *
        Pow(X, p1.second - p2.second) % mod;
}
void dfssl(int u, int X, int C1, int C2, int par) {
  Get(X, C1, C2);
  if (2 * C1 >= C2 && 2 * C2 >= C1) ans = 1ll * ans * X % mod;
  for (pair<int, int> p : v[u]) {
    if (p.first != par && !mark[p.first]) {
      dfssl(p.first, 1ll * X * x[p.second] % mod, C1 + (c[p.second] == 0),
            C2 + (c[p.second] == 1), u);
    }
  }
}
void dfsad(int u, int X, int C1, int C2, int par) {
  Add(X, C1, C2);
  for (pair<int, int> p : v[u]) {
    if (p.first != par && !mark[p.first])
      dfsad(p.first, 1ll * X * x[p.second] % mod, C1 + (c[p.second] == 0),
            C2 + (c[p.second] == 1), u);
  }
}
void solve(int u) {
  dfsz(u);
  u = dfsc(u, SZ[u] / 2);
  mark[u] = 1;
  for (pair<int, int> p : v[u]) {
    if (!mark[p.first]) {
      dfssl(p.first, x[p.second], c[p.second] == 0, c[p.second] == 1, u);
      dfsad(p.first, x[p.second], c[p.second] == 0, c[p.second] == 1, u);
    }
  }
  fn1.Restart();
  fn2.Restart();
  for (pair<int, int> p : v[u]) {
    if (!mark[p.first]) solve(p.first);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b >> x[i] >> c[i];
    v[a].push_back({b, i});
    v[b].push_back({a, i});
  }
  solve(1);
  cout << ans << endl;
  return 0;
}
