#include <bits/stdc++.h>
using namespace std;
long long ans, res;
int n, tn, sn, cnt, ct, mn, sz[200010], la[200010], del[200010];
struct node {
  int from, to, w, t, nxt;
} G[200010 << 1];
struct cc {
  int x, y, z;
};
struct BIT {
  struct tt {
    int x, y;
    bool operator<(const tt z) const { return x < z.x; }
  } q[200010];
  int ls[200010], cnt;
  void cls() { cnt = 0; }
  void pre() {
    sort(q, q + cnt);
    for (int i = 0; i < cnt; i++) {
      ls[i] = q[i].x;
      if (i) q[i].y = 1ll * q[i - 1].y * q[i].y % 1000000007;
    }
  }
  int get(int x) { return lower_bound(ls, ls + cnt, x) - ls; }
  void ins(int x, int y) { q[cnt++] = tt{x, y}; }
} T1, T2;
inline void add(int x, int y, int w, int t) {
  G[++cnt] = node{x, y, w, t, la[x]};
  la[x] = cnt;
}
vector<cc> a[200010];
void rebuild(int x, int fa) {
  int pre = 0;
  for (auto i : a[x]) {
    int v = i.x, w = i.y, t = i.z;
    if (v == fa) continue;
    if (!pre) {
      add(x, v, w, t), add(v, x, w, t);
      pre = x;
    } else {
      int k = ++tn;
      add(k, v, w, t), add(v, k, w, t);
      add(k, pre, 1, 2), add(pre, k, 1, 2);
      pre = k;
    }
    rebuild(v, x);
  }
}
void findct(int x, int fa) {
  sz[x] = 1;
  for (int i = la[x]; i; i = G[i].nxt) {
    int v = G[i].to;
    if (del[i >> 1] || v == fa) continue;
    findct(v, x);
    sz[x] += sz[v];
    int tmp = max(sz[v], sn - sz[v]);
    if (tmp < mn) {
      ct = i;
      mn = tmp;
    }
  }
}
void gao(int x, int fa, int a, int b, long long c) {
  if (x <= n) T1.ins(2 * a - b, c), T2.ins(2 * b - a, c);
  for (int i = la[x]; i; i = G[i].nxt)
    if (!del[i >> 1] && G[i].to != fa)
      gao(G[i].to, x, a + (G[i].t == 0), b + (G[i].t == 1),
          c * G[i].w % 1000000007);
}
long long qpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return res;
}
void get(int x, int fa, int a, int b, long long c) {
  if (x <= n) {
    int t = T1.get(b - 2 * a);
    res = res * qpow(c, t) % 1000000007 * (t ? T1.q[t - 1].y : 1) % 1000000007;
    t = T2.get(a - 2 * b);
    res = res * qpow(c, t) % 1000000007 * (t ? T2.q[t - 1].y : 1) % 1000000007;
  }
  for (int i = la[x]; i; i = G[i].nxt)
    if (!del[i >> 1] && G[i].to != fa)
      get(G[i].to, x, a + (G[i].t == 0), b + (G[i].t == 1),
          c * G[i].w % 1000000007);
}
void dfs(int x) {
  int u = G[x].from, v = G[x].to;
  if (sz[u] < sz[v]) swap(u, v);
  del[x >> 1] = 1;
  T1.cls();
  T2.cls();
  gao(u, -1, 0, 0, 1);
  T1.pre();
  T2.pre();
  get(v, -1, G[x].t == 0, G[x].t == 1, G[x].w);
  int tot = sn;
  sn = tot - sz[v];
  mn = 0x3f3f3f3f;
  findct(u, -1);
  if (mn != 0x3f3f3f3f) dfs(ct);
  sn = sz[v];
  mn = 0x3f3f3f3f;
  findct(v, -1);
  if (mn != 0x3f3f3f3f) dfs(ct);
}
void pre(int x, int fa) {
  sz[x] = 1;
  for (auto i : a[x])
    if (i.x != fa) {
      pre(i.x, x);
      sz[x] += sz[i.x];
      ans = ans * qpow(i.y, 1ll * (n - sz[i.x]) * sz[i.x]) % 1000000007;
    }
}
namespace IO {
bool IOerror = 0;
inline char nc() {
  static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 100000, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror) return;
  if (ch == '-') sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
  if (sign) x = -x;
}
}  // namespace IO
using namespace IO;
int main(int argc, char const *argv[]) {
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y, z, w;
    read(x), read(y);
    read(z), read(w);
    a[x].push_back(cc{y, z, w});
    a[y].push_back(cc{x, z, w});
  }
  ans = res = cnt = 1;
  pre(1, -1);
  tn = n;
  rebuild(1, -1);
  sn = tn;
  mn = 0x3f3f3f3f;
  findct(1, -1);
  dfs(ct);
  printf("%lld\n", ans * qpow(res, 1000000007 - 2) % 1000000007);
  return 0;
}
