#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 98, M = 1e9 + 7;
int add(int a, int b) { return (a += b) >= M ? a - M : a; }
int mul(int a, int b) { return a * 1LL * b % M; }
const pair<int, int> I(0, 1);
pair<int, int> meld(const pair<int, int>& a, const pair<int, int>& b) {
  return pair<int, int>(a.first + b.first, mul(a.second, b.second));
}
int qpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) r = mul(r, a);
  return r;
}
vector<pair<int, int>> g[N];
bitset<N> dead;
int sz[N], ans, bns;
struct fen {
  pair<int, int> fen[4 * N];
  int base, n;
  void init(int _n) {
    n = 4 * _n + 1;
    base = 2 * _n + 1;
    fill_n(fen + 1, n, I);
  }
  void upd(int x, int v) {
    for (int r = x + base; r <= n; r += r & -r)
      fen[r] = meld(fen[r], pair<int, int>(1, v));
  }
  pair<int, int> qry(int x) {
    pair<int, int> ret(I);
    for (int r = x + base; r; r ^= r & -r) ret = meld(ret, fen[r]);
    return ret;
  }
} fa, fb;
void getsz(int u, int p = 0) {
  sz[u] = 1;
  for (const auto& e : g[u]) {
    int v = e.first;
    if (dead[v] || v == p) continue;
    getsz(v, u);
    sz[u] += sz[v];
  }
}
int cent(int u, int n, int p = 0) {
  for (const auto& e : g[u]) {
    int v = e.first;
    if (dead[v] || v == p) continue;
    if (sz[v] > n / 2) return cent(v, n, u);
  }
  return u;
}
vector<pair<pair<int, int>, int>> cx;
void dfs(int u, int p, int x1, int y1, int sx) {
  {
    int a = 2 * y1 - x1, b = y1 - 2 * x1;
    cx.emplace_back(pair<int, int>(-a, -b), sx);
    pair<int, int> ra = fa.qry(a), rb = fb.qry(b - 1);
    int aa = mul(qpow(sx, ra.first), ra.second);
    int bb = mul(qpow(sx, rb.first), rb.second);
    ans = mul(ans, aa);
    bns = mul(bns, bb);
  }
  for (const auto& e : g[u]) {
    int v = e.first;
    if (dead[v] || v == p) continue;
    int x = e.second >> 1;
    int c = e.second & 1;
    dfs(v, u, x1 + c, y1 + !c, mul(sx, x));
  }
}
void DC(int u) {
  getsz(u);
  int n = sz[u];
  u = cent(u, n);
  fa.init(n);
  fb.init(n);
  fa.upd(0, 1);
  fb.upd(0, 1);
  for (const auto& e : g[u]) {
    int v = e.first;
    if (dead[v]) continue;
    int x = e.second >> 1;
    int c = e.second & 1;
    cx.clear();
    dfs(v, u, c, !c, x);
    for (const auto& p : cx) {
      fa.upd(p.first.first, p.second);
      fb.upd(p.first.second, p.second);
    }
  }
  dead[u] = 1;
  for (const auto& e : g[u]) {
    int v = e.first;
    if (dead[v]) continue;
    DC(v);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int u, v, x, c;
    cin >> u >> v >> x >> c;
    g[u].emplace_back(v, x << 1 | c);
    g[v].emplace_back(u, x << 1 | c);
  }
  ans = bns = 1;
  DC(1);
  cout << mul(ans, qpow(bns, M - 2)) << endl;
}
