#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=300050;
vector<pair<int,char>> E[N];
int n,m,x[N],y[N];
char s[N],c[N];
void SubTask1(){
    //deque<pair<pair<int,int>,char>> q;
    pair<pair<int,int>,int> res[n];
    for(int i=0;i<n;i++)res[i].first.first=0,res[i].first.second=0,res[i].second=0;
    pair<pair<int,int>,char> st;
    for(int i=0;i<n-1;i++){
        if(c[i]=='S')st={{x[i],y[i]},c[i]};
    }
    long long ans=0;
    long long cnt0=0,cnt1=0,cnt2=0,cnt3=0;
    for(auto e:E[st.first.first]){
        if(e.second=='O')cnt0++;
        if(e.second=='I')cnt1++;
        for(auto r:E[e.first]){
            if(r.first==st.first.first||e.second==st.first.second)continue;
            if((e.second=='I'&&r.second=='O')||(e.second=='O'&&r.second=='I'))ans++;
        }
    }
    for(auto e:E[st.first.second]){
        if(e.second=='O')cnt2++;
        if(e.second=='I')cnt3++;
        for(auto r:E[e.first]){
            if(r.first==st.first.second||e.second==st.first.first)continue;
            if((e.second=='I'&&r.second=='O')||(e.second=='O'&&r.second=='I'))ans++;
        }
    }
    ans+=cnt0*cnt3+cnt1*cnt2;
    printf("%i",ans);
}
void SubTask2(){
    //printf("cetvorka");
    int ans=0;
    for(int i=0;i<n;i++){
        pair<pair<int,int>,int> res[n];
        for(int j=0;j<n;j++)res[j]={{0,0},0};
        deque<int> q;
        q.pb(i);
        bool was2[N];
        for(int j=0;j<n;j++)was2[j]=0;
        was2[i]=1;
        while(!q.empty()){
            int x=q[0];
            for(auto e:E[x]){
                if(res[e.first].first.first==0&&res[e.first].first.second==0&&res[e.first].second==0){
                    res[e.first]=res[x];
                    if(e.second=='S')res[e.first].first.first++;
                    if(e.second=='I')res[e.first].first.second++;
                    if(e.second=='O')res[e.first].second++;
                }
                if(!was2[e.first])was2[e.first]=1,q.pb(e.first);
            }
            q.pop_front();
        }
        for(int j=i+1;j<n;j++){
            //if(res[j].first.first==res[j].first.second&&res[j].first.second==res[j].second)printf("%i %i\n",i+1,j+1);
            if(res[j].first.first==res[j].first.second&&res[j].first.second==res[j].second)ans++;
        }
    }
    printf("%i",ans);
}
vector<char> v;
bool was[N];
void DFS3(int c){
    was[c]=1;
    for(auto edge:E[c]){
        if(!was[edge.first]){
            v.pb(edge.second);DFS3(edge.first);
        }
    }
}
void SubTask3(){
    int root=0;
    for(int i=0;i<n;i++)if((int)E[i].size()==1)root=i;
    DFS3(root);
    long long ans=0;
    map<long long,int> cnt;
    cnt[0]=1;
    long long a=0,b=0,d=0;
    for(char c:v){
        a+=c=='S',b+=(c=='I'),d+=(c=='O');
        ans+=cnt[(a+b*N+d*N*2)%(3*N+1)];
        cnt[(a+b*N+d*N*2)%(3*N+1)]++;
        //printf("%lld %lld %lld\n",a,b,d);

    }
    printf("%lld",ans);
}
bool was4[N];
int p[N];
map<long long,int> hsh4,hshmid;
long long cnta=0,cntb=0,cntc=0;
long long z=0,yy=0,w=0;
long long z1=0,yyy=0,w1=0;
long long ra=0,rb=0,rc=0;
long long ans4=0;
void DFS4(int cvor){
    was4[cvor]=1;
    bool bio=0;
    sort(E[cvor].begin(),E[cvor].end());
    for(auto R:E[cvor]){
        if(R.second=='S')ra++;
        if(R.second=='I')rb++;
        if(R.second=='O')rc++;
        long long curhsh=(ra+rb*N+rc*2*N);
        //printf("%i %lld\n",cvor,curhsh);
        ans4+=hshmid[((3*N+1)-curhsh)%(3*N+1)];
        hshmid[(ra+rb*N+rc*2*N)%(3*N+1)]++;
        //if(!bio)ans4+=hshmid[((3*N+1)-curhsh)%(3*N+1)];
        if(!was4[R.first])was4[R.first]=1,DFS4(R.first);
        /*if(!bio&&R.second=='S')z++;
        if(!bio&&R.second=='I')y++;
        if(!bio&&R.second=='O')w++;
        if(bio&&R.second=='S')z1++;
        if(bio&&R.second=='I')y1++;
        if(bio&&R.second=='O')w1++;
        long long tmphsh=z1+y1*N+w1*2*N;
        hsh4[]*/
        bio=1;
        if(R.second=='S')ra--;
        if(R.second=='I')rb--;
        if(R.second=='O')rc--;
    }
}
void SubTask4(){
    int root=0;
    for(int i=0;i<n;i++)if((int)E[i].size()==2)root=i;
    for(int i=0;i<n;i++)was4[i]=0;
    p[root]=-1;
    bool was5[N];
    for(int i=0;i<n;i++)was5[i]=0;
    deque<int> q;
    q.pb(root);
    while(!q.empty()){
        int x=q[0];
        for(auto R:E[x]){
            if(!was5[R.first])p[R.first]=x,q.pb(R.first),was5[R.first]=1;
        }
        q.pop_front();
    }
    /*int useless=0;
    for(int i=0;i<n;i++){
        if((int)E[i].size()==1)useless=i;
    }*/
    hshmid[0]=1;
    DFS4(root);
    printf("%lld",ans4);

}
int main(){
    scanf("%i",&n);
    int cnt=0;
    bool putanja=1;
    for(int i=0;i<n-1;i++){
        scanf("%i %i %c",&x[i],&y[i],&c[i]);
        --x[i],--y[i];
        if(abs(x[i]-y[i])!=1)putanja=0;
        E[x[i]].pb({y[i],c[i]});E[y[i]].pb({x[i],c[i]});
        cnt+=c[i]=='S';
    }
    if(cnt==1){
        SubTask1();
        return 0;
    }
    if(n<=1000){
        SubTask2();
    }else if(putanja)SubTask3();
    else SubTask4();
    return 0;
}