#include <bits/stdc++.h>
using namespace std;
const int N = 100015, MXN = N * 3, Mod = 1e9 + 7;
int n, tot = 1, A[N], B[N], T[N], W[N];
int SZ[N], H[N], Ml[N], M[N], F[MXN];
vector<int> Adj[N];
inline int Power(int a, int b) {
  int ret = 1;
  for (; b; b >>= 1, a = 1LL * a * a % Mod)
    if (b & 1) ret = 1LL * ret * a % Mod;
  return (ret);
}
inline void Add(int i, int val) {
  for (i += N; i < MXN; i += i & -i) F[i] += val;
}
inline int Get(int i) {
  int rt = 0;
  for (i += N - 1; i > 0; i -= i & -i) rt += F[i];
  return (rt);
}
void DFSSZ(int v, int p) {
  SZ[v] = 1;
  for (int i : Adj[v])
    if (i != p) {
      int u = A[i] ^ B[i] ^ v;
      if (M[u]) continue;
      DFSSZ(u, i);
      SZ[v] += SZ[u];
    }
}
inline int Centroid(int v) {
  DFSSZ(v, -1);
  int _n = SZ[v];
  int p = -1, w = 1;
  while (w) {
    w = 0;
    for (int i : Adj[v])
      if (i != p) {
        int u = A[i] ^ B[i] ^ v;
        if (M[u]) continue;
        if (SZ[u] + SZ[u] > _n) {
          p = i;
          v = u;
          w = 1;
          break;
        }
      }
  }
  return (v);
}
void DFS(int v, int p) {
  int cnt = Get(-H[v]);
  tot = 1LL * tot * Power(Ml[v], cnt) % Mod;
  for (int i : Adj[v])
    if (i != p) {
      int u = A[i] ^ B[i] ^ v;
      if (M[u]) continue;
      Ml[u] = 1LL * Ml[v] * W[i] % Mod;
      H[u] = H[v] + T[i];
      DFS(u, i);
    }
}
void DFSAdd(int v, int p, int val) {
  Add(H[v], val);
  for (int i : Adj[v])
    if (i != p) {
      int u = A[i] ^ B[i] ^ v;
      if (M[u]) continue;
      DFSAdd(u, i, val);
    }
}
inline void Solve(int v) {
  Add(0, 1);
  for (int i : Adj[v]) {
    int u = A[i] ^ B[i] ^ v;
    if (M[u]) continue;
    Ml[u] = W[i];
    H[u] = T[i];
    DFS(u, i);
    DFSAdd(u, i, 1);
  }
  Add(0, -1);
  for (int i : Adj[v]) {
    int u = A[i] ^ B[i] ^ v;
    if (M[u]) continue;
    DFSAdd(u, i, -1);
    Ml[u] = W[i];
    H[u] = T[i];
    DFS(u, i);
  }
}
inline void Decompose() {
  queue<int> qu;
  qu.push(1);
  while (qu.size()) {
    int v = qu.front();
    qu.pop();
    v = Centroid(v);
    M[v] = 1;
    Solve(v);
    for (int i : Adj[v])
      if (!M[A[i] ^ B[i] ^ v]) qu.push(A[i] ^ B[i] ^ v);
  }
}
void FNL(int v, int p) {
  SZ[v] = 1;
  for (int i : Adj[v])
    if (i != p) {
      int u = A[i] ^ B[i] ^ v;
      FNL(u, i);
      SZ[v] += SZ[u];
      tot =
          1LL * tot * Power(W[i], 1LL * SZ[u] * (n - SZ[u]) % (Mod - 1)) % Mod;
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d%d", &A[i], &B[i], &W[i], &T[i]);
    if (T[i])
      T[i] = 2;
    else
      T[i] = -1;
    Adj[A[i]].push_back(i);
    Adj[B[i]].push_back(i);
  }
  Decompose();
  memset(M, 0, sizeof(M));
  for (int i = 1; i < n; i++) {
    if (T[i] == 2)
      T[i] = -1;
    else
      T[i] = 2;
  }
  Decompose();
  tot = Power(tot, Mod - 2);
  FNL(1, -1);
  return !printf("%d", tot);
}
