Under construction

Still being built. Things may change

Number of Words

Time limit1sMemory limit1024 MB

Summary
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 SS consisting of lowercase English letters. Because SS 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 (ci,vi)(c_i, v_i). The process of restoring the given 'compressed string' to the string SS is as follows.

  1. The initial string SS is empty.
  2. For i=1,2,⋯ ,Ni=1, 2, \cdots, N, append the character cic_i repeated viv_i times to the end of the string SS, in order.

For example, the string SS restored from the 'compressed string' given as (a,4)(a,4), (b,3)(b,3), (c,2)(c,2), (d,1)(d,1), (a,1)(a,1) is S=aaaabbbccdaS=aaaabbbccda.

Find the number of distinct subsequences of SS. The value can be very large, so output it modulo 998244353. Subsequences XX and YY are different if ∣X∣≠∣Y∣|X| \ne |Y|, or if ∣X∣=∣Y∣|X| = |Y| and Xi≠YiX_i \ne{} Y_i for some ii with 1≤i≤∣X∣1 \le{} i \le{} |X|.

Input

The first line gives N(1≤N≤1,000,000)N (1 \le N \le 1,000,000), the number of ordered pairs in the compressed string of SS.

The next NN lines give the ii-th ordered pair (ci,vi)(c_i,v_i) of the compressed string, in order. cic_i is a lowercase English letter, and vi(1≤vi≤109)v_i (1 \le v_i \le 10^9) is the count. Also, when 1<i1<i, ci−1≠cic_{i-1} \ne c_i.

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.

Examples1

  1. Example 1

    Input
    3
    a 3
    b 3
    c 3
    
    Expected output
    63