#include #include #include using namespace std; const int MOD = 998244353; int main() { int len, n, m; auto getdist = [&](int a, int b) -> int { int r = abs(b-a); return min(r, len-r); }; cin >> len >> n >> m; vector> v(n+m); for(auto& x: v) cin >> x.first >> x.second; vector toleft(n+m, -1), toright(n+m, -1); vector st; // forward for(int qq = 0; qq < 2; qq++) { for(int i = 0; i < n+m; i++) { if(v[i].second == 'H') st.push_back(i); else if(st.size()) { int curr = st.back(); st.pop_back(); toleft[i] = curr; toright[curr] = i; } } } st.clear(); // backward for(int qq = 0; qq < 2; qq++) { for(int i = n+m-1; i >= 0; i--) { if(v[i].second == 'H') st.push_back(i); else if(st.size()) { int curr = st.back(); st.pop_back(); toright[i] = curr; toleft[curr] = i; } } } int64_t dist = 0; int ways = 1; for(int i = 0; i < n+m; i++) { if(toleft[i] >= 0) continue; vector costs; int curr = i; while(toright[curr] >= 0) { int ncurr = toright[curr]; costs.push_back(getdist(v[curr].first, v[ncurr].first)); curr = ncurr; } int64_t evendist = 0; for(int i = 1; i < costs.size(); i += 2) evendist += costs[i]; int64_t odddist = 0; int64_t bestdist = evendist + odddist; int numways = 1; for(int i = 0; i < costs.size(); i += 2) { odddist += costs[i]; evendist -= costs[i+1]; if(odddist + evendist < bestdist) { bestdist = odddist + evendist; numways = 0; } numways += (odddist + evendist) == bestdist; } dist += bestdist; ways = (ways * int64_t(numways)) % MOD; } cout << dist << "\n"; cout << ways << "\n"; }