#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define Inf 1000000001 int main(){ int n,x; cin>>n>>x; vector c; vector v; rep(i,x){ char C; long long V; cin>>C>>V; if(c.size()==0 || c.back()!=C){ c.push_back(C); v.push_back(V); } else{ v.back() += V; } } mint ans = 0; rep(i,2){ char cc; if(i==0)cc = 'B'; else cc = 'R'; long long sum = 0LL; rep(j,c.size()){ if(c[j]==cc)sum += v[j]; } if(sum <= 3)continue; mint t = sum; t *= sum-1; t *= sum-2; t *= sum-3; t /= 24; t /= sum-1; t /= sum-3; ans += t; } map mp; mp[""] = 1; rep(i,c.size()){ map nmp = mp; for(auto a:mp){ string ss = a.first; if(ss.size()>0 && ss.back()==c[i])continue; ss += c[i]; if(ss.size()>=5)continue; nmp[ss] += a.second * v[i]; } swap(mp,nmp); } { mint tt = mp["RBRB"] + mp["BRBR"]; int R = 0,B = 0; rep(i,c.size()){ if(c[i]=='R')R += v[i]; else B += v[i]; } tt /= R-1; tt /= B-1; ans += tt; } cout<