Number of Words
Time limit1sMemory limit1024 MB
Given a run-length compressed string, count the number of distinct subsequences of the fully expanded string, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Math, String matching
- Solved
- No attempts yet
Problem
You are given a string consisting of lowercase English letters. Because can be extremely long, it is given in the form of a 'compressed string'. A 'compressed string' is expressed as a sequence of ordered pairs of the form . The process of restoring the given 'compressed string' to the string is as follows.
- The initial string is empty.
- For , append the character repeated times to the end of the string , in order.
For example, the string restored from the 'compressed string' given as , , , , is .
Find the number of distinct subsequences of . The value can be very large, so output it modulo 998244353. Subsequences and are different if , or if and for some with .
Input
The first line gives , the number of ordered pairs in the compressed string of .
The next lines give the -th ordered pair of the compressed string, in order. is a lowercase English letter, and is the count. Also, when , .
Output
Output the number of distinct subsequences modulo 998244353.
Hint
A subsequence is a string obtained by listing some positions of the given string in their original order.