#include <bits/stdc++.h>
using namespace std;
int getnum() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 100010;
const long long mo = 1000000007;
int n;
int cnt = 0;
int head[N];
struct edge {
  int v, nxt;
  long long w;
  int c;
} vec[N << 1];
int up, py;
int Size, minr, root;
int size[N];
bool vis[N];
int top, TOP;
struct node {
  int a, b;
  long long c;
} st[N], ST[N];
long long ans1 = 1, ans2 = 1;
long long W, SW;
long long ksm(long long a, long long b) {
  long long s = 1;
  while (b) {
    if (b & 1) s = s * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return s;
}
struct BIT {
  long long c1[N << 3], c2[N << 3];
  int lowbit(int x) { return x & (-x); }
  void init() {
    for (int i = 1; i <= up; ++i) c1[i] = 1, c2[i] = 0;
  }
  void clear(int x) {
    while (x <= up) {
      c1[x] = 1;
      c2[x] = 0;
      x += lowbit(x);
    }
  }
  void modify(int x, long long w) {
    while (x <= up) {
      c2[x] += 1;
      c1[x] = c1[x] * w % mo;
      x += lowbit(x);
    }
  }
  long long query1(int x) {
    long long ret = 1;
    while (x) {
      ret = ret * c1[x] % mo;
      x -= lowbit(x);
    }
    return ret;
  }
  long long query2(int x) {
    long long ret = 0;
    while (x) {
      ret += c2[x];
      x -= lowbit(x);
    }
    return ret;
  }
  long long Query1(int l, int r) {
    return query1(r) * ksm(query1(l - 1), mo - 2) % mo;
  }
  long long Query2(int l, int r) { return query2(r) - query2(l - 1); }
} c1, c2;
void addedge(int u, int v, int w, int c) {
  vec[++cnt] = (edge){v, head[u], w, c};
  head[u] = cnt;
}
void get_read() {
  n = getnum();
  for (int i = 1; i <= n - 1; ++i) {
    int u = getnum(), v = getnum(), w = getnum(), c = getnum();
    addedge(u, v, w, c);
    addedge(v, u, w, c);
  }
}
void getroot(int u, int fa) {
  size[u] = 1;
  int ret = 0;
  for (int i = head[u]; i; i = vec[i].nxt) {
    int v = vec[i].v;
    if (v == fa || vis[v]) continue;
    getroot(v, u);
    size[u] += size[v];
    ret = max(ret, size[v]);
  }
  ret = max(ret, Size - size[u]);
  if (ret < minr) minr = ret, root = u;
}
void dfs(int u, int fa, int a, int b, long long c) {
  st[++top] = (node){a, b, c};
  W = W * c % mo;
  for (int i = head[u]; i; i = vec[i].nxt) {
    int v = vec[i].v;
    if (v == fa || vis[v]) continue;
    dfs(v, u, a + (vec[i].c ^ 1), b + vec[i].c, c * vec[i].w % mo);
  }
}
void calc(int u) {
  TOP = 0;
  SW = 1;
  ST[++TOP] = (node){0, 0, 1};
  c1.modify(py, 1);
  c2.modify(py, 1);
  for (int i = head[u]; i; i = vec[i].nxt) {
    int v = vec[i].v;
    if (vis[v]) continue;
    top = 0;
    W = 1;
    dfs(v, u, vec[i].c ^ 1, vec[i].c, vec[i].w);
    ans1 = ans1 * ksm(W, TOP) % mo * ksm(SW, top) % mo;
    SW = SW * W % mo;
    for (int j = 1; j <= top; ++j) {
      int A = st[j].b - st[j].a * 2 - 1 + py,
          B = st[j].a - st[j].b * 2 - 1 + py;
      ans2 = ans2 * c1.Query1(2, A) % mo * ksm(st[j].c, c1.Query2(2, A)) % mo;
      ans2 = ans2 * c2.Query1(2, B) % mo * ksm(st[j].c, c2.Query2(2, B)) % mo;
    }
    for (int j = 1; j <= top; ++j) {
      int A = 2 * st[j].a - st[j].b + py;
      c1.modify(A, st[j].c);
      int B = 2 * st[j].b - st[j].a + py;
      c2.modify(B, st[j].c);
      ST[++TOP] = st[j];
    }
  }
  for (int i = 1; i <= TOP; ++i) {
    int A = 2 * ST[i].a - ST[i].b + py;
    c1.clear(A);
    int B = 2 * ST[i].b - ST[i].a + py;
    c2.clear(B);
  }
}
void DFS(int u) {
  vis[u] = true;
  calc(u);
  for (int i = head[u]; i; i = vec[i].nxt) {
    int v = vec[i].v;
    if (vis[v]) continue;
    minr = Size = size[v];
    root = v;
    getroot(v, v);
    DFS(root);
  }
}
void get_work() {
  up = n * 5;
  py = n * 2 + 2;
  c1.init();
  c2.init();
  minr = Size = n;
  root = 1;
  getroot(1, 1);
  DFS(root);
  printf("%lld", ans1 * ksm(ans2, mo - 2) % mo);
}
int main() {
  get_read();
  get_work();
  return 0;
}
