#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 501000;
int q[N], f[N], vis[N], sz[N], ms[N], col[N];
vector<pair<int, int> > br, e[N];
long long ret = 1, ret2 = 1, dp[N];
int m, n, u[N], v[N], w[N], ww[10], sn;
pair<int, long long> c[N];
int find(int u) {
  int t = 1;
  q[0] = u;
  f[u] = -1;
  for (int i = 0; i < t; i++) {
    u = q[i];
    for (int j = 0; j < e[u].size(); j++) {
      int v = e[u][j].first;
      if (!vis[v] && v != f[u]) f[q[t++] = v] = u;
    }
    ms[q[i]] = 0;
    sz[q[i]] = 1;
  }
  sn = t;
  for (int i = t - 1; i >= 0; i--) {
    ms[q[i]] = max(ms[q[i]], t - sz[q[i]]);
    if (ms[q[i]] * 2 <= t) return q[i];
    sz[f[q[i]]] += sz[q[i]];
    ms[f[q[i]]] = max(ms[f[q[i]]], sz[q[i]]);
  }
  return 0;
}
void pre(int u, int f) {
  sz[u] = 1;
  for (auto p : e[u]) {
    int v = p.first, id = p.second;
    if (v == f) continue;
    pre(v, u);
    sz[u] = sz[u] + sz[v];
    ret = ret * powmod(w[id], (long long)sz[v] * (n - sz[v])) % mod;
  }
}
pair<int, long long> operator*(pair<int, long long> a, pair<int, long long> b) {
  return make_pair(a.first + b.first, a.second * b.second % mod);
}
pair<int, long long> query(int x) {
  pair<int, long long> s = make_pair(0, 1);
  for (; x; x -= x & -x) s = s * c[x];
  return s;
}
void modify(int x, pair<int, long long> s) {
  for (; x <= m; x += x & -x) c[x] = c[x] * s;
}
void dfs(int u, int f, int dep, long long W) {
  pair<int, long long> cw = query(4 * sn - 1 - dep);
  ret2 = (ret2 * powmod(W, cw.first) % mod * cw.second) % mod;
  br.push_back(make_pair(dep, W));
  for (auto p : e[u]) {
    if (p.first == f || vis[p.first]) continue;
    dfs(p.first, u, dep + ww[col[p.second]], W * w[p.second] % mod);
  }
}
void solve(int u) {
  u = find(u);
  m = 4 * sn;
  for (int i = 0; i <= m; i++) c[i] = make_pair(0, 1);
  modify(2 * sn, make_pair(1, 1));
  for (auto p : e[u]) {
    int v = p.first;
    if (vis[v]) continue;
    br.clear();
    dfs(v, u, 2 * sn + ww[col[p.second]], w[p.second]);
    for (auto p : br) modify(p.first, make_pair(1, p.second));
  }
  vis[u] = 1;
  for (auto p : e[u])
    if (!vis[p.first]) solve(p.first);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d%d", u + i, v + i, w + i, col + i);
    e[u[i]].push_back(make_pair(v[i], i));
    e[v[i]].push_back(make_pair(u[i], i));
  }
  pre(1, 0);
  ww[0] = 2;
  ww[1] = -1;
  solve(1);
  for (int i = 1; i < n + 1; i++) vis[i] = 0;
  ww[0] = -1;
  ww[1] = 2;
  solve(1);
  ret = ret * powmod(ret2, mod - 2) % mod;
  printf("%lld\n", ret);
}
