#include <bits/stdc++.h>
using namespace std;
int gi() {
  int x = 0, w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') w = 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return w ? x : -x;
}
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
struct node {
  int x, y, z;
};
int n, sz[N], w[N], vis[N], sum, root, bit[N << 2], ans = 1;
vector<node> E[N];
void getroot(int u, int f) {
  sz[u] = 1;
  w[u] = 0;
  for (auto p : E[u])
    if (p.x != f && !vis[p.x]) {
      getroot(p.x, u);
      sz[u] += sz[p.x];
      w[u] = max(w[u], sz[p.x]);
    }
  w[u] = max(w[u], sum - sz[u]);
  if (w[u] < w[root]) root = u;
}
void dfs(int u, int f, int a, int b, int t, vector<node>& v) {
  v.push_back((node){a * 2 - b, b * 2 - a, t});
  for (auto p : E[u])
    if (p.x != f && !vis[p.x])
      dfs(p.x, u, a + p.y, b + !p.y, 1ll * t * p.z % mod, v);
}
int fastpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return res;
}
void mdf(int x, int v) {
  x += N << 1;
  while (x < N << 2) bit[x] += v, x += x & -x;
}
int qry(int x) {
  x += N << 1;
  int s = 0;
  while (x) s += bit[x], x ^= x & -x;
  return s;
}
int cal(vector<node>& v) {
  int res = 1;
  for (auto p : v) mdf(p.x, 1);
  for (auto p : v) res = 1ll * res * fastpow(p.z, qry(-p.x - 1)) % mod;
  for (auto p : v) mdf(p.x, -1);
  for (auto p : v) mdf(p.y, 1);
  for (auto p : v) res = 1ll * res * fastpow(p.z, qry(-p.y - 1)) % mod;
  for (auto p : v) mdf(p.y, -1);
  res = fastpow(res, mod - 2);
  for (auto p : v) res = 1ll * res * fastpow(p.z, v.size()) % mod;
  return res;
}
void solve(int u) {
  vis[u] = 1;
  vector<node> all, cur;
  for (auto p : E[u])
    if (!vis[p.x]) {
      cur.clear();
      dfs(p.x, u, p.y, !p.y, p.z, cur);
      ans = 1ll * ans * fastpow(cal(cur), mod - 2) % mod;
      for (auto q : cur) all.push_back(q);
    }
  all.push_back((node){0, 0, 1});
  ans = 1ll * ans * cal(all) % mod;
  for (auto p : E[u])
    if (!vis[p.x]) {
      getroot(p.x, 0);
      root = 0;
      sum = sz[p.x];
      getroot(p.x, 0);
      solve(root);
    }
}
int main() {
  n = gi();
  for (int i = 1; i < n; ++i) {
    int u = gi(), v = gi(), w = gi(), c = gi();
    E[u].push_back((node){v, c, w});
    E[v].push_back((node){u, c, w});
  }
  w[0] = sum = n;
  getroot(1, 0);
  solve(root);
  printf("%d\n", ans);
  return 0;
}
