#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int C = 26;
void add(int &x, int y) {
  x += y;
  while (x >= MOD) x -= MOD;
  while (x < 0) x += MOD;
}
int fix(int x) {
  while (x >= MOD) x -= MOD;
  while (x < 0) x += MOD;
  return x;
}
int pw(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % MOD;
    b >>= 1;
    a = 1ll * a * a % MOD;
  }
  return ret;
}
int inv(int a) { return pw(a, MOD - 2); }
int mul(int a, int b) { return (long long)a * b % MOD; }
void mul_self(int &a, int b) { a = mul(a, b); }
const int MAXN = 1e6 + 10;
const int BASE = MAXN / 2;
int n, edges[MAXN], color[MAXN], val[MAXN], sz[MAXN], ans = 1;
vector<int> adj[MAXN];
struct Fen {
  array<int, 2> ft[MAXN];
  vector<array<int, 3>> ops;
  Fen() { fill(ft, ft + MAXN, array<int, 2>{1, 0}); }
  void upd(int i, int x, int y, bool clr = false) {
    if (!clr) {
      ops.push_back({i, inv(x), -y});
    }
    i += BASE;
    while (i < MAXN) {
      mul_self(ft[i][0], x);
      ft[i][1] += y;
      i += i & -i;
    }
  }
  array<int, 2> get(int i) {
    array<int, 2> ret = {1, 0};
    i += BASE;
    while (i > 0) {
      mul_self(ret[0], ft[i][0]);
      ret[1] += ft[i][1];
      i -= i & -i;
    }
    return ret;
  }
  void clear() {
    for (auto [i, x, y] : ops) {
      upd(i, x, y, true);
    }
    ops.clear();
  }
} ft[2];
bool removed[MAXN];
int get_sz(int v, int p) {
  sz[v] = 1;
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (u ^ p && !removed[u]) sz[v] += get_sz(u, v);
  }
  return sz[v];
}
int get_centroid(int v, int p, int N) {
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (u ^ p && !removed[u] && sz[u] > N / 2) return get_centroid(u, v, N);
  }
  return v;
}
void dfs_ans(int v, int p, int prod, array<int, 2> cnt) {
  auto [pp, paths] = ft[0].get(2 * cnt[1] - cnt[0]);
  mul_self(ans, mul(pp, pw(prod, paths)));
  auto [pp2, paths2] = ft[1].get(cnt[1] - 2 * cnt[0] - 1);
  mul_self(ans, inv(mul(pp2, pw(prod, paths2))));
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (removed[u] || u == p) continue;
    ++cnt[color[e]];
    dfs_ans(u, v, mul(prod, val[e]), cnt);
    --cnt[color[e]];
  }
}
void dfs_fill(int v, int p, int prod, array<int, 2> cnt) {
  ft[0].upd(cnt[0] - 2 * cnt[1], prod, 1);
  ft[1].upd(2 * cnt[0] - cnt[1], prod, 1);
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (removed[u] || u == p) continue;
    ++cnt[color[e]];
    dfs_fill(u, v, mul(prod, val[e]), cnt);
    --cnt[color[e]];
  }
}
void decompose(int v) {
  int N = get_sz(v, -1);
  v = get_centroid(v, -1, N);
  ft[0].upd(0, 1, 1);
  ft[1].upd(0, 1, 1);
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (removed[u]) continue;
    array<int, 2> cnt = array<int, 2>();
    ++cnt[color[e]];
    dfs_ans(u, v, val[e], cnt);
    dfs_fill(u, v, val[e], cnt);
  }
  ft[0].clear();
  ft[1].clear();
  removed[v] = 1;
  for (int e : adj[v]) {
    int u = edges[e] ^ v;
    if (removed[u]) continue;
    decompose(u);
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b >> val[i] >> color[i];
    --a;
    --b;
    edges[i] = a ^ b;
    adj[a].push_back(i);
    adj[b].push_back(i);
  }
  decompose(0);
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int te = 1;
  for (int w = 1; w <= te; w++) {
    solve();
  }
  return 0;
}
