#include <bits/stdc++.h>
using namespace std;
const int bl = 250000, mm = 1000000007, lim = 500000;
struct edge {
  int t, next, c;
  long long v;
} g[210001];
long long ans, p[110001];
int i, j, k, n, q[110001], now, sz[110001], f[110001], h[110001], tot, t1, t2,
    v[110001], a[110001][2], w[110001];
struct bit {
  long long c[510001];
  int ts[510001];
  void cle() { memset(ts, 0, sizeof(ts)); }
  void changep(int x, long long v) {
    for (; x <= lim; x += x & (-x)) {
      if (ts[x] != now) ts[x] = now, c[x] = 1;
      c[x] = c[x] * v % mm;
    }
  }
  long long queryp(int x) {
    long long res = 1;
    for (; x; x -= x & (-x)) {
      if (ts[x] != now) ts[x] = now, c[x] = 1;
      res = res * c[x] % mm;
    }
    return res;
  }
  void changes(int x, long long v) {
    for (; x <= lim; x += x & (-x)) {
      if (ts[x] != now) ts[x] = now, c[x] = 0;
      c[x] = (c[x] + v) % mm;
    }
  }
  long long querys(int x) {
    long long res = 0;
    for (; x; x -= x & (-x)) {
      if (ts[x] != now) ts[x] = now, c[x] = 0;
      res = (res + c[x]) % mm;
    }
    return res;
  }
} cp1, cp2, cs1, cs2;
long long pw(long long b, long long p) {
  long long res = 1;
  for (; p; p >>= 1, b = b * b % mm)
    if (p & 1) res = res * b % mm;
  return res;
}
void addedge(int x, int y, int v, int c) {
  g[++tot].t = y;
  g[tot].next = h[x];
  h[x] = tot;
  g[tot].v = v;
  g[tot].c = c;
  g[++tot].t = x;
  g[tot].next = h[y];
  h[y] = tot;
  g[tot].v = v;
  g[tot].c = c;
}
void dfs0(int x, int fa) {
  int i;
  q[++q[0]] = x;
  sz[x] = 1;
  f[x] = 0;
  for (i = h[x]; i; i = g[i].next)
    if (fa != g[i].t && !v[g[i].t])
      dfs0(g[i].t, x), sz[x] += sz[g[i].t], f[x] = max(f[x], sz[g[i].t]);
}
int find(int x) {
  int i, k;
  q[0] = 0;
  dfs0(x, 0);
  for (k = x, i = 1; i <= q[0]; i++) {
    f[q[i]] = max(f[q[i]], sz[x] - sz[q[i]]);
    if (f[k] > f[q[i]]) k = q[i];
  }
  return k;
}
void dfs1(int x, int fa) {
  int i;
  w[++w[0]] = x;
  for (i = h[x]; i; i = g[i].next)
    if (g[i].t != fa && !v[g[i].t]) {
      p[g[i].t] = p[x] * g[i].v % mm;
      a[g[i].t][0] = a[x][0];
      a[g[i].t][1] = a[x][1];
      a[g[i].t][g[i].c]++;
      dfs1(g[i].t, x);
    }
}
void work(int x) {
  int i, j, z;
  long long k, t, t1;
  v[x] = 1;
  now++;
  for (i = h[x]; i; i = g[i].next) {
    if (v[g[i].t]) continue;
    w[0] = 0;
    p[g[i].t] = g[i].v;
    a[g[i].t][g[i].c] = 1;
    a[g[i].t][g[i].c ^ 1] = 0;
    dfs1(g[i].t, x);
    for (j = 1; j <= w[0]; j++) {
      z = w[j];
      if (min(a[z][0], a[z][1]) * 2 >= max(a[z][0], a[z][1]))
        ans = ans * p[z] % mm;
      k = cp1.queryp(2 * a[z][0] - a[z][1] + bl);
      t = cs1.querys(2 * a[z][0] - a[z][1] + bl);
      ans = ans * pw(p[z], t) % mm * k % mm;
      k = cp2.queryp(a[z][0] - 2 * a[z][1] - 1 + bl);
      t = cs2.querys(a[z][0] - 2 * a[z][1] - 1 + bl);
      t1 = pw(p[z], t) * k % mm;
      ans = ans * pw(t1, mm - 2) % mm;
    }
    for (j = 1; j <= w[0]; j++) {
      z = w[j];
      cp1.changep(a[z][1] - 2 * a[z][0] + bl, p[z]);
      cs1.changes(a[z][1] - 2 * a[z][0] + bl, 1);
      cp2.changep(2 * a[z][1] - a[z][0] + bl, p[z]);
      cs2.changes(2 * a[z][1] - a[z][0] + bl, 1);
    }
  }
  for (i = h[x]; i; i = g[i].next)
    if (!v[g[i].t]) work(find(g[i].t));
}
int main() {
  scanf("%d", &n);
  tot = 0;
  memset(h, 0, sizeof(h));
  for (i = 1; i < n; i++)
    scanf("%d%d%d%d", &j, &k, &t1, &t2), addedge(j, k, t1, t2);
  ans = 1;
  cp1.cle();
  cp2.cle();
  cs1.cle();
  cs2.cle();
  now = 0;
  work(find(1));
  printf("%d\n", (int)ans);
  return 0;
}
