#include <bits/stdc++.h>
using namespace std;
struct data {
  long long num;
  long long w;
};
bool mark[100005];
long long sum, num;
int a[100005];
vector<int> e[100005];
vector<data> cnt;
int n, ans[4];
int judge(int d) {
  while (d != 0) {
    int tmp = d % 10;
    if (tmp != 4 && tmp != 7) {
      return 0;
    }
    d /= 10;
  }
  return 1;
}
void dfs1(int d) {
  if (d == 3) {
    if (a[0] == a[1] && a[1] == a[2]) {
      if (cnt[a[0]].w == 1) {
        if (cnt[a[0]].num < 3) {
          return;
        } else {
          sum += cnt[a[0]].num * (cnt[a[0]].num - 1) * (cnt[a[0]].num - 2);
        }
      } else {
        if (cnt[a[0]].num < 2) {
          return;
        } else {
          sum += cnt[a[0]].num * cnt[a[0]].w * (cnt[a[0]].w - 1) *
                 (cnt[a[0]].num - 1) * cnt[a[0]].w;
          if (cnt[a[0]].num > 2) {
            sum += pow(1.0 * cnt[a[0]].w, 3) * cnt[a[0]].num *
                   (cnt[a[0]].num - 1) * (cnt[a[0]].num - 2);
          }
        }
      }
    } else {
      if (a[0] != a[1] && a[1] != a[2] && a[2] != a[0]) {
        sum += cnt[a[0]].num * cnt[a[0]].w * cnt[a[1]].num * cnt[a[1]].w *
               cnt[a[2]].num * cnt[a[2]].w;
      } else {
        int tmp = -1;
        for (int i = 0; i < 3; i++) {
          for (int j = i + 1; j < 3; j++) {
            if (a[i] == a[j]) {
              tmp = a[i];
            }
          }
        }
        int tmp1;
        for (int i = 0; i < 3; i++) {
          if (a[i] != tmp) {
            tmp1 = a[i];
          }
        }
        if (cnt[tmp].num > 1) {
          sum += cnt[tmp].num * pow(1.0 * cnt[tmp].w, 2) * (cnt[tmp].num - 1) *
                 cnt[tmp1].num * cnt[tmp1].w;
        }
        if (cnt[tmp].w > 1) {
          if (a[0] == tmp1) {
            sum += cnt[tmp].num * cnt[tmp].w * (cnt[tmp].w - 1) *
                   cnt[tmp1].num * cnt[tmp1].w;
          }
        }
      }
    }
  } else {
    int r = cnt.size();
    for (int i = 0; i < r; i++) {
      a[d] = i;
      dfs1(d + 1);
    }
  }
}
void dfs(int i) {
  num++;
  mark[i] = 1;
  for (int j = 0; j < e[i].size(); j++) {
    if (!mark[e[i][j]]) {
      dfs(e[i][j]);
    }
  }
}
int main() {
  int u, v, w;
  while (scanf("%d", &n) != EOF) {
    memset(mark, 0, sizeof(mark));
    for (int i = 0; i < n - 1; i++) {
      scanf("%d%d%d", &u, &v, &w);
      u--;
      v--;
      if (!judge(w)) {
        e[u].push_back(v);
        e[v].push_back(u);
      }
    }
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++) {
      num = 0;
      if (!mark[i]) {
        dfs(i);
      }
      a[num]++;
    }
    sum = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] != 0) {
        data tmp;
        tmp.num = a[i];
        tmp.w = i;
        cnt.push_back(tmp);
      }
    }
    dfs1(0);
    cout << sum << endl;
  }
  return 0;
}
