CF1558D.Top-Notch Insertions

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider the insertion sort algorithm used to sort an integer sequence [a1,a2,,an][a_1, a_2, \ldots, a_n] of length nn in non-decreasing order.

For each ii in order from 22 to nn , do the following. If aiai1a_i \ge a_{i-1} , do nothing and move on to the next value of ii . Otherwise, find the smallest jj such that ai<aja_i < a_j , shift the elements on positions from jj to i1i-1 by one position to the right, and write down the initial value of aia_i to position jj . In this case we'll say that we performed an insertion of an element from position ii to position jj .

It can be noticed that after processing any ii , the prefix of the sequence [a1,a2,,ai][a_1, a_2, \ldots, a_i] is sorted in non-decreasing order, therefore, the algorithm indeed sorts any sequence.

For example, sorting [4,5,3,1,3][4, 5, 3, 1, 3] proceeds as follows:

  • i=2i = 2 : a2a1a_2 \ge a_1 , do nothing;
  • i=3i = 3 : j=1j = 1 , insert from position 33 to position 11 : [3,4,5,1,3][3, 4, 5, 1, 3] ;
  • i=4i = 4 : j=1j = 1 , insert from position 44 to position 11 : [1,3,4,5,3][1, 3, 4, 5, 3] ;
  • i=5i = 5 : j=3j = 3 , insert from position 55 to position 33 : [1,3,3,4,5][1, 3, 3, 4, 5] .

You are given an integer nn and a list of mm integer pairs (xi,yi)(x_i, y_i) . We are interested in sequences such that if you sort them using the above algorithm, exactly mm insertions will be performed: first from position x1x_1 to position y1y_1 , then from position x2x_2 to position y2y_2 , ..., finally, from position xmx_m to position ymy_m .

How many sequences of length nn consisting of (not necessarily distinct) integers between 11 and nn , inclusive, satisfy the above condition? Print this number modulo 998244353998\,244\,353 .

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t1051 \le t \le 10^5 ). Description of the test cases follows.

The first line of each test case contains two integers nn and mm ( 2n21052 \le n \le 2 \cdot 10^5 ; 0m<n0 \le m < n ) — the length of the sequence and the number of insertions.

The ii -th of the following mm lines contains two integers xix_i and yiy_i ( 2x1<x2<<xmn2 \le x_1 < x_2 < \ldots < x_m \le n ; 1yi<xi1 \le y_i < x_i ). These lines describe the sequence of insertions in chronological order.

It is guaranteed that the sum of mm over all test cases does not exceed 21052 \cdot 10^5 . Note that there is no constraint on the sum of nn of the same kind.

输出格式

For each test case, print the number of sequences of length nn consisting of integers from 11 to nn such that sorting them with the described algorithm produces the given sequence of insertions, modulo 998244353998\,244\,353 .

输入输出样例

  • 输入#1

    3
    3 0
    3 2
    2 1
    3 1
    5 3
    3 1
    4 1
    5 3

    输出#1

    10
    1
    21

说明/提示

In the first test case, the algorithm performs no insertions — therefore, the initial sequence is already sorted in non-decreasing order. There are 1010 such sequences: [1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3][1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3] .

In the second test case, the only sequence satisfying the conditions is [3,2,1][3, 2, 1] .

In the third test case, [4,5,3,1,3][4, 5, 3, 1, 3] is one of the sought sequences.

首页