#include <bits/stdc++.h>   using namespace std;   void faster() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const int MAXN = 2e5 + 1;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   const long double PI = acos(-1);   long long n, a[MAXN], dp[MAXN], d[MAXN], cnt[MAXN], ans;   vector<pair<int, bool>> g[MAXN];   bool is_lucky(int x) {     bool res = 1;     while (x) {       res &= (((x % 10) == 7) | ((x % 10) == 4));       x /= 10;     }     return res;   }   void dfs(int v, int pr = 0) {     cnt[v] = 1;     for (pair<int, int> x : g[v]) {       int to = x.first, cost = x.second;       if (to == pr) continue;       dfs(to, v);       cnt[v] += cnt[to];       if (cost)         d[v] += cnt[to];       else         d[v] += d[to];     }   }   void dfs_a(int v, int pr, long long res) {     ans += (res * (res - 1));     for (pair<int, int> x : g[v]) {       int to = x.first, cost = x.second;       if (pr == to) continue;       if (cost)         dfs_a(to, v, d[to] + (cnt[1] - cnt[to]));       else         dfs_a(to, v, res);     }   }   int main() {     faster();     cin >> n;     for (int i = 1, v, u, w; i < n; ++i) {       cin >> u >> v >> w;       w = is_lucky(w);       g[u].push_back({v, w});       g[v].push_back({u, w});     }     dfs(1);     dfs_a(1, 0, d[1]);     cout << ans;     return 0;   }   