atcoder#ARC124A. [ARC124A] LR Constraints

[ARC124A] LR Constraints

Score : 300300 points

Problem Statement

We have NN cards arranged in a row from left to right. We will write an integer between 11 and KK (inclusive) on each of these cards, which are initially blank.

Given are KK restrictions numbered 11 through KK. Restriction ii is composed of a character cic_i and an integer kik_i. If cic_i is L, the kik_i-th card from the left in the row must be the leftmost card on which we write ii. If cic_i is R, the kik_i-th card from the left in the row must be the rightmost card on which we write ii.

Note that for each integer ii from 11 through KK, there must be at least one card on which we write ii.

Find the number of ways to write integers on the cards under the KK restrictions, modulo 998244353998244353.

Constraints

  • 1N,K10001 \leq N,K \leq 1000
  • cic_i is L or R.
  • 1kiN1 \leq k_i \leq N
  • kikjk_i \neq k_j if iji \neq j.

Input

Input is given from Standard Input in the following format:

NN KK

c1c_1 k1k_1

\vdots

cKc_K kKk_K

Output

Find the number of ways to write integers on the cards under the KK restrictions in the Problem Statement, modulo 998244353998244353.

3 2
L 1
R 2
1
  • The only way to meet the two restrictions is to write 1,2,11, 2, 1 from left to right on the three cards.
30 10
R 6
R 8
R 7
R 25
L 26
L 13
R 14
L 11
L 23
R 30
343921442
  • Be sure to find the count modulo 998244353998244353.