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

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>

int n,r=-1,q=-1,r1=0,r2=0,q1=0,q2=0,BRS=0,BRI=0,BRO=0;
vector <int> A; vector <parii> B(1);
bool svi_plus1 = true;

int Podzadatak1()
{
    for ( int i = 1; i < n; i++ )
    {
        if ( (B[i].first == r && B[i].second != q) || (B[i].second == r && B[i].first != q) )
        {
            if ( A[max(B[i].first,B[i].second)] == 2 )
            {
                r1++;
            }
            if ( A[max(B[i].first,B[i].second)] == 3 )
            {
                r2++;
            }
        }
        if ( (B[i].first == q && B[i].second != r) || (B[i].second == q && B[i].first != r) )
        {
            if ( A[max(B[i].first,B[i].second)] == 2 )
            {
                q1++;
            }
            if ( A[max(B[i].first,B[i].second)] == 3 )
            {
                q2++;
            }
        }
    }
    cout << r1*q2+r2*q1;
}
int Podzadatak2()
{
    cout << 0;
}
int Podzadatak3()
{
    vector < vector < vector <int> > > DP;
    DP.resize(BRS+1);
    for ( int i = 0; i < BRS; i++ )
    {
        DP[i].resize(BRI+1);
        for ( int j = 0; j < BRI; j++ )
        {
            DP[i][j].resize(BRO+1);
            fill( DP[i][j].begin(), DP[i][j].end(), -1 );
        }
    }
    int SIO[4] = { 0 }; int m,ms=0,mi=0,mo=0;
    DP[SIO[1]][SIO[2]][SIO[3]]++;
    for ( int i = 2; i <= n; i++ )
    {
        SIO[A[i]]++;
        m = min( SIO[1], min( SIO[2], SIO[3] ) );
        if ( m >= 0 )
        {
            SIO[1] -= m; SIO[2] -= m; SIO[3] -= m;
        }
        DP[SIO[1]][SIO[2]][SIO[3]]++;
        //cout << " S = " << SIO[1] << " I = " << SIO[2] << " O = " << SIO[3] << " --> "<< DP[SIO[1]][SIO[2]][SIO[3]] << "\n";
        ms = max( ms, SIO[1] );
        mi = max( mi, SIO[2] );
        mo = max( mo, SIO[3] );
    }

    //cout << " ms = " << ms << " mi = " << mi << " mo = " << mo << "\n";

    int rez=0,br;
    for ( int i = 0; i <= ms; i++ )
    {
        for ( int j = 0; j <= mi; j++ )
        {
            for ( int k = 0; k <= mo; k++ )
            {
                br = DP[i][j][k];
                br = (br*(br+1))/2;
                rez = br>0?rez+br:rez;
                //cout << " i = " << i << " j = " << j << " k = " << k << " --> "<< br << "\n";
                //cout << " rez = " << rez << "\n";
            }
        }
    }
    cout << rez;
}
int Podzadatak4( int i, int SIO[4], vector < vector < vector <int> > > DP )
{
    if ( i > n )
    {
        return 0;
    }
    if ( i > 1 )
    {
        SIO[A[i]]++;
        int m = min( SIO[1], min( SIO[2], SIO[3] ) );
        if ( m >= 0 )
        {
            SIO[1] -= m; SIO[2] -= m; SIO[3] -= m;
        }
        DP[SIO[1]][SIO[2]][SIO[3]]++;
    }

    //cout << i << " " << " S = " << SIO[1] << " I = " << SIO[2] << " O = " << SIO[3] << " --> "<< DP[SIO[1]][SIO[2]][SIO[3]] << "\n";

    int K[4];
    K[1] = SIO[1]; K[2] = SIO[2]; K[3] = SIO[3];
    vector < vector < vector <int> > > P;
    P = DP;
    int br = DP[SIO[1]][SIO[2]][SIO[3]];
    br += Podzadatak4(i*2,SIO,DP);
    DP = P;
    SIO[1] = K[1]; SIO[2] = K[2]; SIO[3] = K[3];
    br += Podzadatak4((i*2)+1,SIO,DP);
    DP = P;
    SIO[1] = K[1]; SIO[2] = K[2]; SIO[3] = K[3];

    return br;
}
int Podzadatak5()
{
    cout << 0;
}

void Provera()
{
    bool moze = true; int brS = 0;
    for ( int i = 1; i <= n; i++ )
    {
        if ( A[i] == 1 )
        {
            brS++;
        }
    }
    if ( brS == 1 )
    {
        Podzadatak1();
        return;
    }
    if ( svi_plus1 )
    {
        Podzadatak3();
        return;
    }
    moze = true;
    for ( int i = 1; i < n; i++ )
    {
        if ( max(B[i].first,B[i].second)>>1 != min(B[i].first,B[i].second) )
        {
            moze = false;
            break;
        }
    }
    if ( moze )
    {
        vector < vector < vector <int> > > DP;
        DP.resize(BRS+1);
        for ( int i = 0; i < BRS; i++ )
        {
            DP[i].resize(BRI+1);
            for ( int j = 0; j < BRI; j++ )
            {
                DP[i][j].resize(BRO+1);
                fill( DP[i][j].begin(), DP[i][j].end(), -1 );
            }
        }
        int SIO[4] = { 0 };
        DP[0][0][0] = 0;
        cout << Podzadatak4(1,SIO,DP);
        return;
    }
    if ( n <= 1000 )
    {
        Podzadatak2();
        return;
    }
    Podzadatak5();
    return;
}

int main()
{
    ios;

    char c; int p,d;

    cin >> n;
    A.resize(n+1); fill( A.begin(), A.end(), 0 );
    for ( int i = 1; i < n; i++ )
    {
        cin >> p >> d >> c;
        B.push_back(mp(p,d));
        if ( abs(p-d) > 1 )
        {
            svi_plus1 = false;
        }
        if ( c == 'S' )
        {
            r = p; q = d;
            A[max(p,d)] = 1;
            BRS++;
        }
        else if ( c == 'I' )
        {
            A[max(p,d)] = 2;
            BRI++;
        }
        else
        {
            A[max(p,d)] = 3;
            BRO++;
        }
    }

    Provera();

    return 0;
}