#include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T rnd(T l, T r) {
  return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __Z = 0;
inline void __Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20) __Ot();
  if (x < 0) __sr[++__C] = '-', x = -x;
  while (__z[++__Z] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__Z], --__Z)
    ;
  __sr[++__C] = '\n';
}
void file() {}
inline void chktime() {}
long long ksm(long long x, int y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x % 1000000007ll)
    if (y & 1) ret = ret * x % 1000000007ll;
  return ret;
}
long long inv(long long x) { return ksm(x, 1000000007ll - 2); }
}  // namespace my_std
using namespace my_std;
int n;
struct hh {
  int t;
  long long w;
  int nxt;
  int col;
} edge[101100 << 1];
int head[101100], ecnt;
void make_edge(int f, int t, long long w, int col) {
  edge[++ecnt] = (hh){t, w, head[f], col};
  head[f] = ecnt;
  edge[++ecnt] = (hh){f, w, head[t], col};
  head[t] = ecnt;
}
int T;
struct BIT {
  long long prod[101100 << 2];
  int size[101100 << 2];
  int mark[101100 << 2];
  void mul(int x, long long w) {
    x += n * 2;
    while (x <= n * 4) {
      if (mark[x] != T) size[x] = 0, prod[x] = 1;
      ++size[x], prod[x] = prod[x] * w % 1000000007ll;
      mark[x] = T;
      x += (x & (-x));
    }
  }
  void query(int x, long long &w1, int &w2) {
    w1 = 1, w2 = 0;
    x += n * 2;
    while (x) {
      if (mark[x] == T) w1 = w1 * prod[x] % 1000000007ll, w2 += size[x];
      x -= (x & (-x));
    }
  }
} T1, T2;
long long ans = 1;
bool vis[101100];
int size[101100], mn, root, tot;
void findroot(int x, int fa) {
  size[x] = 1;
  int S = -1;
  for (int i = head[x]; i; i = edge[i].nxt)
    if (edge[i].t != fa && !vis[edge[i].t]) {
      findroot(edge[i].t, x);
      size[x] += size[edge[i].t];
      chkmax(S, size[edge[i].t]);
    }
  chkmax(S, tot - size[x]);
  if (chkmin(mn, S)) root = x;
}
struct hhh {
  int aa, bb;
  long long w;
};
hhh s[101100];
int cnt;
void dfs(int x, int fa, int a, int b, long long w) {
  s[++cnt] = (hhh){2 * a - b, 2 * b - a, w};
  for (int i = head[x]; i; i = edge[i].nxt)
    if (edge[i].t != fa && !vis[edge[i].t]) {
      if (edge[i].col)
        dfs(edge[i].t, x, a, b + 1, w * edge[i].w % 1000000007ll);
      else
        dfs(edge[i].t, x, a + 1, b, w * edge[i].w % 1000000007ll);
    }
}
void calc(int x) {
  ++T;
  T1.mul(0, 1);
  T2.mul(0, 1);
  for (int i = head[x]; i; i = edge[i].nxt)
    if (!vis[edge[i].t]) {
      cnt = 0;
      if (edge[i].col)
        dfs(edge[i].t, 0, 0, 1, edge[i].w);
      else
        dfs(edge[i].t, 0, 1, 0, edge[i].w);
      for (int i = (1); i <= (cnt); i++) {
        long long w1;
        int w2;
        T1.query(s[i].aa, w1, w2);
        ans = ans * w1 % 1000000007ll * ksm(s[i].w, w2) % 1000000007ll;
        T2.query(-s[i].bb - 1, w1, w2);
        ans = ans * inv(w1 * ksm(s[i].w, w2) % 1000000007ll) % 1000000007ll;
      }
      for (int i = (1); i <= (cnt); i++)
        T1.mul(-s[i].aa, s[i].w), T2.mul(s[i].bb, s[i].w);
    }
}
void solve(int x) {
  vis[x] = 1;
  calc(x);
  int all = tot;
  for (int i = head[x]; i; i = edge[i].nxt)
    if (!vis[edge[i].t]) {
      tot = size[edge[i].t];
      if (size[edge[i].t] > size[x]) tot = all - size[x];
      mn = 1e9;
      findroot(edge[i].t, 0);
      solve(root);
    }
}
int main() {
  file();
  int x, y, z, c;
  read(n);
  for (int i = (1); i <= (n - 1); i++) read(x, y, z, c), make_edge(x, y, z, c);
  tot = n;
  mn = 1e9;
  findroot(1, 0);
  solve(root);
  cout << ans;
  return 0;
}
