CF BUDDY
← Problems·

1886D · Monocarp and the Set

2100 · combinatorics, data structures, math

Problem: Monocarp has nn numbers 1,2,,n1, 2, \dots, n and a set (initially empty). He adds his numbers to this set nn times in some order. During each step, he adds a new number (which has not been present in the set before). In other words, the sequence of added numbers is a permutation of length nn.

Every time Monocarp adds an element into the set except for the first time, he writes out a character:

  • if the element Monocarp is trying to insert becomes the maximum element in the set, Monocarp writes out the character >;
  • if the element Monocarp is trying to insert becomes the minimum element in the set, Monocarp writes out the character <;
  • if none of the above, Monocarp writes out the character ?.

You are given a string ss of n1n-1 characters, which represents the characters written out by Monocarp (in the order he wrote them out). You have to process mm queries to the string. Each query has the following format:

  • ii cc — replace sis_i with the character cc.

Both before processing the queries and after each query, you have to calculate the number of different ways to order the integers 1,2,3,,n1, 2, 3, \dots, n such that, if Monocarp inserts the integers into the set in that order, he gets the string ss. Since the answers might be large, print them modulo 998244353998244353.

Input Format: The first line contains two integers nn and mm (2n31052 \le n \le 3 \cdot 10^5; 1m31051 \le m \le 3 \cdot 10^5).

The second line contains the string ss, consisting of exactly n1n-1 characters <, > and/or ?.

Then mm lines follow. Each of them represents a query. Each line contains an integer ii and a character cc (1in11 \le i \le n-1; cc is either <, >, or ?).

Output Format: Both before processing the queries and after each query, print one integer — the number of ways to order the integers 1,2,3,,n1, 2, 3, \dots, n such that, if Monocarp inserts the integers into the set in that order, he gets the string ss. Since the answers might be large, print them modulo 998244353998244353.

Note: In the first example, there are three possible orderings before all queries:

  • 3,1,2,5,4,63, 1, 2, 5, 4, 6;
  • 4,1,2,5,3,64, 1, 2, 5, 3, 6;
  • 4,1,3,5,2,64, 1, 3, 5, 2, 6.

After the last query, there is only one possible ordering:

  • 3,5,4,6,2,13, 5, 4, 6, 2, 1.

Sample Cases

Case 1

Input

6 4
<?>?>
1 ?
4 <
5 <
1 >

Output

3
0
0
0
1

Case 2

Input

2 2
>
1 ?
1 <

Output

1
0
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback