#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, ans;
vector<vector<pair<int, int> > > gr;
map<pair<int, int>, int > pref;

void dfs(int x, int p, int sC, int iC, int tC){
	ans += pref[make_pair(sC-iC, sC-tC)];
	pref[make_pair(sC-iC, sC-tC)]++;
	for(auto i : gr[x]){
		if(i.first == p){
			continue;
		}
		if(i.second == 0) {
			dfs(i.first, x, sC+1, iC, tC);
		} else if(i.second == 2){
			dfs(i.first, x, sC, iC, tC+1);
		} else {
			dfs(i.first, x, sC, iC+1,tC);
		}
	}
	pref[make_pair(sC-iC, sC-tC)]--;
}

int conv(char a){
	if(a == 'S'){
		return 0;
	}
	if(a=='O'){
		return 2;
	}
	return 1;
}

void dfs2(int x, int p, int sC, int iC, int tC){
	ans += (sC and sC==tC and sC==iC);
	//cerr << x << ' ' << sC << ' ' << iC << ' ' << tC << '\n';
	for(auto i : gr[x]){
		if(i.first == p){
			continue;
		}
		if(i.second == 0) {
			dfs2(i.first, x, sC+1, iC, tC);
		} else if(i.second == 2){
			dfs2(i.first, x, sC, iC, tC+1);
		} else {
			dfs2(i.first, x, sC, iC+1,tC);
		}
	}

}
signed main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> n;
	gr.resize(n, vector<pair<int, int>>());
	int sA = 0;
	for(int i = 0; i < n-1; ++i){
		int u, v;
		char a;
		cin >> u >> v >> a;
		--u, v--;
		sA += (a=='S');
		gr[u].push_back({v, conv(a)});
		gr[v].push_back({u, conv(a)});
	}
	if(sA == 1){
		for(int i = 0; i < n; ++i){
			for(auto j : gr[i]){
				if(j.second != conv('S')){
					continue;
				}
				int tmpO1 = 0, tmpI1 = 0, tmpO2 = 0, tmpI2 = 0;
				for(auto j1 : gr[j.first]){
					if(j1.second == conv('I')){
						tmpI1++;
					} else if(j1.second == conv('O')){
						tmpO1++;
					}
				}
				for(auto j1 : gr[i]){
					if(j1 != j){
						if(j1.second == conv('I')){
							tmpI2++;
						} else if(j1.second == conv('O')){
							tmpO2++;
						}
					}
				}
				//cerr << j.first << ' ' << gr[j.first][0].second << ' ';
				ans += tmpO1*tmpI2 + tmpO1*tmpI1;
				dfs2(i, -1, 0, 0, 0);
			}
		}
		cout << ans;
		return 0;
	}
	if(n <= 1000){
		ans = 0;
		for(int i = 0; i < n; ++i){
			dfs2(i, -1, 0, 0, 0);
		}
		cout << ans/2;
		return 0;
	}
	for(int i = 0; i < n; ++i){
		if(gr[i].size() == 1){
			dfs(i, -1, 0, 0, 0);
			break;
		}
	}
	cout << ans;
}