## Algorithm

E. Lucky Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

One day Petya encountered a tree with n vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with n vertexes is an undirected connected graph that has exactly n - 1 edges.

Petya wondered how many vertex triples (i, j, k) exists that on the way from i to j, as well as on the way from i to k there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1, 2, 3) is not equal to the triple (2, 1, 3) and is not equal to the triple (1, 3, 2).

Find how many such triples of vertexes exist.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of tree vertexes. Next n - 1 lines contain three integers each: ui vi wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — the pair of vertexes connected by the edge and the edge's weight.

Output

On the single line print the single number — the answer.

Please do not use the %lld specificator to read or write 64-bit numbers in ะก++. It is recommended to use the cin, cout streams or the %I64d specificator.

Examples
input
Copy
`41 2 43 1 21 4 7`
output
Copy
`16`
input
Copy
`41 2 41 3 471 4 7447`
output
Copy
`24`
Note

The 16 triples of vertexes from the first sample are: (1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2).

In the second sample all the triples should be counted: 4·3·2 = 24.

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 1;
bool vis[N];
int n, f[N], ff[N], siz[N];
vector<vector<pair<int, int> > > g;

bool is_lucky(int x) {
while(x != 0) {
if(x % 10 != 4 && x % 10 != 7)
return false;
x /= 10;
}
return true;
}

void DFS1(int u) {
vis[u] = true;
++siz[u];

for(int i = 0, v; i < g[u].size(); ++i) {
v = g[u][i].first;
if(!vis[v]) {
DFS1(v);
siz[u] += siz[v];

if(is_lucky(g[u][i].second))
f[u] += siz[v];
else
f[u] += f[v];
}
}
}

void DFS2(int u) {
vis[u] = true;

for(int i = 0, v; i < g[u].size(); ++i) {
v = g[u][i].first;
if(!vis[v]) {
if(is_lucky(g[u][i].second))
ff[v] = n - siz[v];
else
ff[v] = ff[u] + f[u] - f[v];

DFS2(v);
}
}
}

int main() {
scanf("%d", &n);
g.resize(n);
for(int i = 0, a, b, c; i < n - 1; ++i) {
scanf("%d %d %d", &a, &b, &c);
--a, --b;
g[a].push_back({b, c});
swap(a, b);
g[a].push_back({b, c});
}

DFS1(0);
memset(vis, false, sizeof vis);
DFS2(0);

long long res = 0;
for(int i = 0; i < n; ++i)
res += 1ll * f[i] * (f[i] - 1) + 1ll * ff[i] * (ff[i] - 1) + 2ll * f[i] * ff[i];

printf("%lld\n", res);

return 0;
}``````
Copy The Code &

Input

cmd
4
1 2 4
3 1 2
1 4 7

Output

cmd
16