#include "bits/stdc++.h"
#pragma GCC target("avx,avx2")
#pragma GCC optimize("Ofast,unroll-loops")
#define FOR(a, b, c) for(a = b; a <=c; a++)
#define ROF(a, b, c) for(a = b;a >= c; a--)
#define mem0(A) memset(A,0,sizeof(A))
#define mem1(A) memset(A,-1,sizeof(A))
#define memf(A) memset(A,false,sizeof(A))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define GEN(T) template<typename T>
#define EB emplace_back
#define SORT(x) sort(all(x))
#define all(x) x.begin(), x.end()
#define newl '\n'
#define IOF ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using UL  =  unsigned long int;
using UI  =  unsigned int;
using ULL =  unsigned long long int;
using LI  =  long int;
using LL  =  long long;
using LF  =  double;
using LLF =  long double;
const int mod1 = 1000000007;
const int mod2 = 998244353;
const double PI = acos(-1);
typedef vector<vector<int>> adjList;

LL ans ;

adjList graph;
int vis[200001];
inline void dfs(int v,int par){
    vis[v] = 1;
    for(auto child : graph[v]){
        if(vis[child]==0){
            dfs(child,v);
        }
        else if(vis[child]==1){
            ans++;
        }
    }
    vis[v] = 2;
}

LL solver(LL a,LL x){
    LL res = 1;
    LL mods = mod2 * 1ll;
    while(x>0){
        if(x&1){
            res = (res * a)%mods;
        }
        a = (a*a)%mods;
        x >>= 1;
    }
    return res;
}

int main(){
        IOF
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        int ntc=1,tc,n,i,j;
        
        //cin>>ntc;
        //cout<<fixed<<setprecision(16);
        FOR(tc,1,ntc){
            //cout<<"Case #"<<tc<<": ";
            cin>>n;
            graph.resize(n);
            FOR(i,0,n-1){
                cin>>j;
                j--;
                graph[i].PB(j);
            }


            FOR(i,0,n-1){
                if(vis[i]==0){
                    dfs(i,-1);
                }
            }

            cout<<(solver(2ll,ans)-1)%mod2;

        }
}
#include "bits/stdc++.h"
using namespace std;

#define ll long long int 
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j)
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }

ll n;
vll r, g, b;

ll getAns(vll& a, vll& b)
{
  ll ans = big;
  for(ll e : b)
  {
    vll::iterator itr = lower_bound(a.begin(), a.end(), e);
    if(itr == a.end())
      continue;
    ll na = *itr;
    ans = min(ans, na-e);
  }
  for(ll e : a)
  {
    vll::iterator itr = lower_bound(b.begin(), b.end(), e);
    if(itr == b.end())
      continue;
    ll nb = *itr;
    ans = min(ans, nb-e);
  }
  return ans;
}

ll anotherWay(vll& a, vll& b, vll& c)
{
  return getAns(a, c) + getAns(b, c);
}

void solve()
{
  cin>>n;
  char c;
  ll ip;
  loop0(i, 2*n)
  {
    cin>>ip>>c;
    if(c == 'R')
      r.pb(ip);
    if(c == 'G')
      g.pb(ip);
    if(c == 'B')
      b.pb(ip);
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(b.begin(), b.end());
  if(r.size()%2 == 0 && g.size()%2 == 0 && b.size()%2 == 0)
  {
    cout<<"0\n";
    return;
  }
  ll ans = 0;
  if(r.size()%2 == 0)
  {
    ans = getAns(g, b);
    if(r.size())
      ans = min(ans , anotherWay(g, b, r));
  }
  else if(g.size()%2 == 0)
  {
    ans = getAns(r, b);
    if(g.size())
      ans = min(ans, anotherWay(r, b, g));
  }
  else
  {
    ans = getAns(r, g);
    if(b.size())
      ans = min(ans, anotherWay(r, g, b));
  }
  cout<<ans<<"\n";
}

int main()
{
  solve();
}
