#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, siz[100005], ans = 1;
bool vis[100005];
int fir[100005], tot;
struct edge {
  int nxt, to, w, col;
} e[100005 << 1];
void line(int x, int y, int z, int c) {
  e[++tot] = (edge){fir[x], y, z, c}, fir[x] = tot;
}
int T;
struct BIT {
  int arr[100005 << 2], siz[100005 << 2], tim[100005 << 2];
  void mul(int i, int v) {
    for (i += 2 * n; i <= 4 * n; i += i & -i) {
      if (tim[i] ^ T) siz[i] = 0, tim[i] = T, arr[i] = 1;
      siz[i]++, arr[i] = 1ll * arr[i] * v % mod;
    }
  }
  void qry(int i, int &s, int &prd) {
    for (i += 2 * n, s = 0, prd = 1; i; i -= i & -i)
      if (tim[i] == T) s += siz[i], prd = 1ll * prd * arr[i] % mod;
  }
} T1, T2;
void getrt(int u, int ff, int tsz, int &g) {
  siz[u] = 1;
  bool flg = 1;
  for (int i = fir[u], v; i; i = e[i].nxt)
    if (!vis[v = e[i].to] && v != ff)
      getrt(v, u, tsz, g), siz[u] += siz[v], flg &= siz[v] << 1 <= tsz;
  if (flg && (tsz - siz[u]) << 1 <= tsz) g = u;
}
int getrt(int u, int tsz) { return getrt(u, 0, tsz, u), u; }
struct node {
  int a, b, w;
} q[100005];
int cnt;
void dfs(int u, int ff, int a, int b, int w) {
  q[++cnt] = (node){2 * a - b, 2 * b - a, w};
  for (int i = fir[u], v; i; i = e[i].nxt)
    if (!vis[v = e[i].to] && v != ff)
      dfs(v, u, a + e[i].col, b + !e[i].col, 1ll * w * e[i].w % mod);
}
int Pow(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod) b & 1 && (s = 1ll * s * a % mod);
  return s;
}
void TDC(int u, int tsz) {
  vis[u] = 1, ++T, T1.mul(0, 1), T2.mul(0, 1);
  for (int j = fir[u], v; j; j = e[j].nxt)
    if (!vis[v = e[j].to]) {
      cnt = 0, dfs(v, u, e[j].col, !e[j].col, e[j].w);
      for (int i = 1, s, w; i <= cnt; i++) {
        T1.qry(q[i].a, s, w), ans = 1ll * ans * w % mod * Pow(q[i].w, s) % mod;
        T2.qry(-q[i].b - 1, s, w),
            ans =
                1ll * ans * Pow(1ll * w * Pow(q[i].w, s) % mod, mod - 2) % mod;
      }
      for (int i = 1; i <= cnt; i++)
        T1.mul(-q[i].a, q[i].w), T2.mul(q[i].b, q[i].w);
    }
  for (int i = fir[u], v; i; i = e[i].nxt)
    if (!vis[v = e[i].to]) {
      int sz = siz[v] < siz[u] ? siz[v] : tsz - siz[u];
      TDC(getrt(v, sz), sz);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1, x, y, z, c; i < n; i++)
    scanf("%d%d%d%d", &x, &y, &z, &c), line(x, y, z, c), line(y, x, z, c);
  TDC(getrt(1, n), n);
  printf("%d\n", ans);
}
