#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int n, ans = 1;
bool used[N];
vector<pair<int, pair<int, int> > > g[N];
struct item {
  item *l, *r;
  int key, val, prior, cnt, prod;
  item() {}
  item(int x, int y) {
    key = x;
    prior = rand() ^ (rand() << 15);
    cnt = 1;
    prod = val = y;
    l = r = nullptr;
  }
};
typedef item *pitem;
pitem t = nullptr;
inline int cnt(pitem v) { return v ? v->cnt : 0; }
inline int prod(pitem v) { return v ? v->prod : 1; }
inline void upd_cnt(pitem v) {
  if (v) {
    v->cnt = cnt(v->l) + 1 + cnt(v->r);
    v->prod = ((1ll * prod(v->l) * v->val) % MOD * prod(v->r)) % MOD;
  }
}
void Merge(pitem &v, pitem l, pitem r) {
  if (!l || !r) {
    v = l ? l : r;
    return;
  }
  if (l->prior > r->prior) {
    Merge(l->r, l->r, r);
    v = l;
  } else {
    Merge(r->l, l, r->l);
    v = r;
  }
  upd_cnt(v);
}
void Split(pitem v, int key, pitem &l, pitem &r) {
  if (!v) {
    l = r = nullptr;
    return;
  }
  if (key < v->key) {
    Split(v->l, key, l, v->l);
    r = v;
  } else {
    Split(v->r, key, v->r, r);
    l = v;
  }
  upd_cnt(v);
}
inline void Insert(pitem &v, int x, int y) {
  pitem tr;
  Split(v, x, v, tr);
  Merge(v, v, new item(x, y));
  Merge(v, v, tr);
}
void Delete(pitem &v) {
  if (!v) {
    return;
  }
  Delete(v->l);
  Delete(v->r);
  delete v;
}
pair<int, int> Get(pitem v, int x) {
  pitem tl;
  Split(v, x, tl, v);
  auto it = make_pair(cnt(tl), prod(tl));
  Merge(v, tl, v);
  return it;
}
inline int binpow(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1) {
      res = (1ll * res * a) % MOD;
    }
    a = (1ll * a * a) % MOD;
    n >>= 1;
  }
  return res;
}
int dfs(int v, int size, int &center, int pr = -1) {
  int sz = 1;
  for (auto it : g[v]) {
    int to = it.first;
    if (to != pr && !used[to]) {
      sz += dfs(to, size, center, v);
    }
  }
  if (center == -1 && sz >= size / 2) {
    center = v;
  }
  return sz;
}
void dfs1(int v, int pr, int prod, int bb, int rr) {
  Insert(t, bb - 2 * rr, prod);
  if (min(bb, rr) * 2 >= max(bb, rr)) {
    ans = (1ll * ans * prod) % MOD;
  }
  for (auto it : g[v]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (to != pr && !used[to]) {
      dfs1(to, v, (1ll * prod * zz) % MOD, bb + (c == 0), rr + (c == 1));
    }
  }
}
void dfs2(int v, int pr, int prod, int b, int r) {
  auto it = Get(t, 2 * r - b);
  int tkol = it.first, tprod = it.second;
  ans = ((1ll * ans * (1ll * tprod * binpow(prod, tkol) % MOD)) % MOD);
  for (auto it : g[v]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (to != pr && !used[to]) {
      dfs2(to, v, (1ll * prod * zz) % MOD, b + (c == 0), r + (c == 1));
    }
  }
}
void dfs3(int v, int pr, int prod, int bb, int rr) {
  Insert(t, 2 * bb - rr, prod);
  for (auto it : g[v]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (to != pr && !used[to]) {
      dfs3(to, v, (1ll * prod * zz) % MOD, bb + (c == 0), rr + (c == 1));
    }
  }
}
void dfs4(int v, int pr, int prod, int b, int r) {
  auto it = Get(t, r - 2 * b - 1);
  int tkol = it.first, tprod = it.second;
  ans = ((1ll * ans *
          binpow(((1ll * tprod * binpow(prod, tkol)) % MOD), MOD - 2)) %
         MOD);
  for (auto it : g[v]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (to != pr && !used[to]) {
      dfs4(to, v, (1ll * prod * zz) % MOD, b + (c == 0), r + (c == 1));
    }
  }
}
void build(int v) {
  int tmp = 0, center = -1;
  dfs(v, dfs(v, 0, tmp), center);
  for (auto it : g[center]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (!used[to]) {
      dfs2(to, center, zz, (c == 0), (c == 1));
      dfs1(to, center, zz, (c == 0), (c == 1));
    }
  }
  t = nullptr;
  for (auto it : g[center]) {
    int to = it.first, zz = it.second.first, c = it.second.second;
    if (!used[to]) {
      dfs4(to, center, zz, (c == 0), (c == 1));
      dfs3(to, center, zz, (c == 0), (c == 1));
    }
  }
  t = nullptr;
  used[center] = true;
  for (auto it : g[center]) {
    int to = it.first;
    if (!used[to]) {
      build(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y, z, c;
    cin >> x >> y >> z >> c;
    g[x].push_back(make_pair(y, make_pair(z, c)));
    g[y].push_back(make_pair(x, make_pair(z, c)));
  }
  build(1);
  cout << ans;
}
