#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
  if (fu) v = -v;
}
const int MO = 1e9 + 7;
long long pw(long long x, long long y) {
  long long t = 1;
  for (; y; y >>= 1) {
    if (y & 1) t = t * x % MO;
    x = x * x % MO;
  }
  return t;
}
bool leaf[600010];
char s[600010];
int nxt[600010], x, y, w, d[600010], val[600010], a[600010], n, hed[600010],
    too[600010], nedge;
void ae(int x, int y, int w) {
  nxt[++nedge] = hed[x];
  hed[x] = nedge;
  too[nedge] = y;
  val[nedge] = w;
}
struct node {
  long long x, y;
} f[300010];
void dfs(int x, int l) {
  leaf[x] = 1;
  for (int i = hed[x]; i; i = nxt[i]) {
    int y = too[i];
    if (y == l) continue;
    dfs(y, x);
    leaf[x] = 0;
  }
  if (leaf[x]) {
    f[x].x = 0, f[x].y = 0;
    return;
  }
  long long t = 1, tt = 0, I = pw(d[x], MO - 2);
  for (int i = hed[x]; i; i = nxt[i]) {
    int y = too[i];
    if (y == l) tt = val[i] * I % MO;
  }
  for (int i = hed[x]; i; i = nxt[i]) {
    int y = too[i];
    if (y == l) continue;
    (t -= f[y].x * I) %= MO;
    (tt += (val[i] + f[y].y) * I) %= MO;
  }
  f[x].x = pw(t, MO - 2) * I % MO;
  f[x].y = tt * pw(t, MO - 2) % MO;
}
int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    getint(x), getint(y), getint(w);
    ++x, ++y;
    ae(x, y, w), ae(y, x, w);
    ++d[x], ++d[y];
  }
  dfs(1, 0);
  cout << (f[1].y + MO) % MO << endl;
  return 0;
}
