#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define endl "\n"

using namespace std;

vector<vector<pair<int,char>>> g;
vector<bool> visited;
int n;
ll ans=0;

void dfs1(int node, int S, int I, int O)
{
    if(I>1 || O>1)
        return;

    if(S==I && I==O)
        ans++;

    for(auto x : g[node]) {
        if(!visited[x.first]) {
            visited[x.first]=true;

            if(x.second=='I')
                I++;
            if(x.second=='O')
                O++;

            dfs1(x.first,S,I,O);

            if(x.second=='I')
                I--;
            if(x.second=='O')
                O--;

        }
    }
}

void dfs(int root, int node, int S, int I, int O)
{
    if(S==I && I==O && O && root<node)
        ans++;

    for(auto x : g[node]) {
        if(!visited[x.first]) {
            visited[x.first]=true;

            if(x.second=='S')
                S++;
            if(x.second=='I')
                I++;
            if(x.second=='O')
                O++;

            dfs(root,x.first,S,I,O);

            if(x.second=='S')
                S--;
            if(x.second=='I')
                I--;
            if(x.second=='O')
                O--;

        }
    }
}

void solve()
{
    cin >> n;
    g.resize(n);
    visited.resize(n);

    int S=0;
    int s1,s2;
    bool krpenje=true;
    vector<char> krpi(n-1);

    for(int i=0;i<n-1;i++) {
        int u,v; cin >> u >> v;
        char w; cin >> w;
        u--; v--;

        if(w=='S') {
            S++;
            s1=u; s2=v;
        }
        if(abs(u-v)==1) {
            krpi[min(u,v)]=w;
        }
        else
            krpenje=false;

        g[u].push_back(make_pair(v,w));
        g[v].push_back(make_pair(u,w));
    }

    if(S==1) {
        fill(visited.begin(), visited.end(), false);
        visited[s1]=true;
        dfs1(s2,1,0,0);

        fill(visited.begin(), visited.end(), false);
        visited[s2]=true;
        dfs1(s1,1,0,0);

        int i1=0,o1=0;
        int i2=0,o2=0;

        for(auto x:g[s1]) {
            if(x.second=='I')
                i1++;
            if(x.second=='O')
                o1++;
        }
        for(auto x:g[s2]) {
            if(x.second=='I')
                i2++;
            if(x.second=='O')
                o2++;
        }

        ans+=i1*o2+i2*o1;

        cout << ans << endl;
        return;
    }

    if(krpenje) {

        unordered_map<string,int> m;

        int A=0,B=0,C=0;
        m["0 0 0"]=1;

        string cur="0 0 0";

        for(char c:krpi) {

            if(c=='S')
                A++;
            if(c=='I')
                B++;
            if(c=='O')
                C++;


            if(A<=B && A<=C) {
                B-=A;
                C-=A;
                A=0;
            }
            else if(B<=A && B<=C) {
                A-=B;
                C-=B;
                B=0;
            }
            else if(C<=A && C<=B) {
                A-=C;
                B-=C;
                C=0;
            }

            cur=to_string(A);
            cur.push_back(' ');
            cur+=to_string(B);
            cur.push_back(' ');
            cur+=to_string(C);

            cout << cur << endl;

            ans+=m[cur];
            m[cur]++;
        }

        cout << ans << endl;
        return;
    }

    for(int i=0;i<n;i++) {
        fill(visited.begin(), visited.end(), false);
        visited[i]=true;
        dfs(i,i,0,0,0);
    }

    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int t=1; //cin >> t;
    while(t--)
        solve();

    return 0;
}