#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e9 + 13;
const long long MOD = 1e9 + 7;
inline long long mmul(long long a, long long b) { return (a * b) % MOD; }
inline long long madd(long long a, long long b) { return (a + b) % MOD; }
struct Bit {
  long long f[100005 * 4];
  void upd(int i, long long d) {
    for (; i < 100005 * 4; i = (i | (i + 1))) f[i] += d;
  }
  long long get(int i) const {
    long long res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1) res += f[i];
    return res;
  }
} Tree;
long long p[31];
long long binpow(long long a, long long b) {
  p[0] = a;
  for (int i = 1; i < 31; i++) p[i] = mmul(p[i - 1], p[i - 1]);
  long long ret = 1;
  for (int i = 31 - 1; i >= 0; i--) {
    if (b >= (1 << i)) {
      b -= (1 << i);
      ret = mmul(ret, p[i]);
    }
  }
  return ret;
}
long long modinv(long long a) { return binpow(a, MOD - 2); }
int N;
int tot = 0;
vector<pair<int, pair<int, int> > > adj[100005];
bool vis[100005];
int sz[100005];
vector<int> guys;
void dfs1(int u, int pa) {
  if (pa == -1) guys.clear();
  guys.push_back(u);
  sz[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (v == pa) continue;
    if (vis[v]) continue;
    dfs1(v, u);
    sz[u] += sz[v];
  }
  if (pa == -1) tot = sz[u];
}
int centroid(int u, int pa) {
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (v == pa) continue;
    if (vis[v]) continue;
    if (sz[v] * 2 >= tot) return centroid(v, u);
  }
  return u;
}
pair<pair<int, int>, long long> vals[100005];
void dfs3(int u, int pa) {
  if (pa == -1) vals[u] = make_pair(pair<int, int>(0, 0), 1);
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    long long type = adj[u][i].second.second;
    long long wt = adj[u][i].second.first;
    if (v == pa) continue;
    if (vis[v]) continue;
    long long nr = 1LL - type;
    long long nb = type;
    vals[v] = make_pair(pair<int, int>(vals[u].first.first + nr - 2LL * nb,
                                       vals[u].first.second + nb - 2LL * nr),
                        mmul(vals[u].second, wt));
    dfs3(v, u);
  }
}
bool cmp(int a, int b) {
  if (vals[a].first.first == vals[b].first.first)
    return vals[a].first.second < vals[b].first.second;
  return vals[a].first.first < vals[b].first.first;
}
long long go(int cent, int targ) {
  if (targ == -1) dfs3(cent, -1);
  vector<pair<pair<int, int>, long long> > queries;
  int OFFSET = 200000;
  int MAXV = OFFSET * 2;
  for (int i = 0; i < guys.size(); i++) {
    int v = guys[i];
    int vx = vals[v].first.first;
    int vy = vals[v].first.second;
    long long mult = vals[v].second;
    queries.push_back(
        make_pair(pair<int, int>(-vx + OFFSET, -vy + OFFSET), mult));
  }
  sort(guys.begin(), guys.end(), cmp);
  sort(queries.begin(), queries.end());
  long long ret = 1LL;
  int cur = 0;
  for (int i = 0; i < queries.size(); i++) {
    int xcoord = queries[i].first.first - OFFSET;
    while ((cur < guys.size()) && (vals[guys[cur]].first.first <= xcoord)) {
      Tree.upd(vals[guys[cur]].first.second + OFFSET, 1);
      cur++;
    }
    int ycoord = queries[i].first.second;
    ret = mmul(ret, binpow(queries[i].second, Tree.get(ycoord)));
  }
  cur = 0;
  for (int i = 0; i < queries.size(); i++) {
    int xcoord = queries[i].first.first - OFFSET;
    while ((cur < guys.size()) && (vals[guys[cur]].first.first <= xcoord)) {
      Tree.upd(vals[guys[cur]].first.second + OFFSET, -1);
      cur++;
    }
  }
  return ret;
}
long long solve(int u, int pa) {
  dfs1(u, -1);
  int cent = centroid(u, pa);
  long long ret = 1;
  ret = mmul(ret, go(cent, -1));
  vis[cent] = 1;
  for (int i = 0; i < adj[cent].size(); i++) {
    int v = adj[cent][i].first;
    if (vis[v]) continue;
    dfs1(v, -1);
    ret = mmul(ret, modinv(go(cent, i)));
  }
  for (int i = 0; i < adj[cent].size(); i++) {
    int v = adj[cent][i].first;
    if (vis[v]) continue;
    ret = mmul(ret, solve(v, cent));
  }
  return ret;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    adj[a].push_back(make_pair(b, pair<int, int>(c, d)));
    adj[b].push_back(make_pair(a, pair<int, int>(c, d)));
  }
  cout << solve(1, -1) << endl;
}
