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

#define pb push_back

int n;
vector<pair<int, int>> g[300005];

int sRes;
int su, sv;

int nRes;

map<pair<int, pair<int, int>>, int> lanacCnt;
int lanacRes;

void sDfs(int node, int par, int iCnt, int oCnt)
{
    if(iCnt + oCnt > 2){return;}
    if(iCnt == 1 && oCnt == 1){++sRes;}

    for(auto c : g[node]){
        if(c.first != par){
            if(c.second == 'I'){sDfs(c.first, node, iCnt + 1, oCnt);}
            else if(c.second == 'O'){sDfs(c.first, node, iCnt, oCnt + 1);}
        }
    }
}

void sSub()
{
    int sui = 0, suo = 0;
    for(auto c : g[su]){
        if(c.second == 'I'){++sui;}
        else if(c.second == 'O'){++suo;}
    }

    int svi = 0, svo = 0;
    for(auto c : g[sv]){
        if(c.second == 'I'){++svi;}
        else if(c.second == 'O'){++svo;}
    }

    sRes = sui * svo + suo * svi;
    sDfs(su, sv, 0, 0);
    sDfs(sv, su, 0, 0);

    cout << sRes << "\n";
}

void nDfs(int node, int par, int sCnt, int iCnt, int oCnt)
{
    if(sCnt > 0 && sCnt == iCnt && iCnt == oCnt){++nRes;}

    for(auto c : g[node]){
        if(c.first != par){
            if(c.second == 'S'){nDfs(c.first, node, sCnt + 1, iCnt, oCnt);}
            else if(c.second == 'I'){nDfs(c.first, node, sCnt, iCnt + 1, oCnt);}
            else if(c.second == 'O'){nDfs(c.first, node, sCnt, iCnt, oCnt + 1);}
        }
    }
}

void n1000()
{
    for(int i = 1; i <= n; ++i){
        nDfs(i, -1, 0, 0, 0);
    }

    cout << nRes / 2 << "\n";
}

void lanac()
{
    char last[300005];
    for(int i = 2; i <= n; ++i){
        for(auto c : g[i]){
            if(c.first == i - 1){last[i] = c.second; break;}
        }
    }

    int sCnt = 0, iCnt = 0, oCnt = 0;
    for(int i = 2; i <= n; ++i){
        if(last[i] == 'S'){++sCnt;}
        else if(last[i] == 'I'){++iCnt;}
        else if(last[i] == 'O'){++oCnt;}

        int maxi = max(sCnt, max(iCnt, oCnt));
        lanacRes += lanacCnt[{maxi - sCnt, {maxi - iCnt, maxi - oCnt}}];

        int mini = min(sCnt, min(iCnt, oCnt));
        ++lanacCnt[{sCnt - mini, {iCnt - mini, oCnt - mini}}];
    }

    cout << lanacRes << "\n";
}

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

    cin >> n;

    bool jeLanac = true;
    int sCnt = 0;
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        char x;

        cin >> u >> v >> x;

        g[u].pb({v, x});
        g[v].pb({u, x});

        if(x == 'S'){++sCnt; su = u; sv = v;}
        if(abs(u - v) != 1){jeLanac = false;}
    }

    if(sCnt == 1){
        sSub();
    }
    else if(n <= 1000){
        n1000();
    }
    else if(jeLanac){
        lanac();
    }
    else{
        n1000();
    }

    return 0;
}