Shuffle

Time limit1sMemory limit128 MB

Summary
Given a song-play log and playlist size s, count starting offsets that split the log into blocks of length s (first/last possibly shorter) with no repeated song inside any block.
Level

Medium6 of 10

Topics
Sliding window, Array, Implementation
Solved
No attempts yet

Problem

Jugyeong loves listening to music, and when she does she uses the shuffle feature of her music player. The player generates a random permutation of the songs in the playlist and plays every song in that order; once all songs have been played, it creates a new permutation (another shuffle) and keeps playing.

Jugyeong has a record of the songs that have been played so far. However, she realized that the record does not begin at the very first song that was ever played. In other words, the record is a contiguous segment that starts at some moment after playback began and ends at some later moment.

Let ss be the number of songs in the playlist. The record can be split into consecutive blocks of length ss, where only the first and the last block may be shorter than ss (they correspond to the tail and the head of a shuffle, respectively). Because each block is part of a single permutation produced by one shuffle, no song may appear more than once within the same block.

Such a way of splitting the record is fixed by the position of the first block boundary, which is a single value from 00 to s−1s-1. Count the number of ways to split the record so that every block satisfies the condition. This count is exactly the number of possibilities for how the next shuffle could continue.

Input

The first line contains the number of test cases TT. TT is at most 100100.

For each test case, the first line contains two integers ss and nn (1≤s,n≤1000001 \le s, n \le 100000). ss is the number of songs in the playlist, and nn is the number of songs recorded so far.

The second line contains nn integers x1,x2,…,xnx_1, x_2, \ldots, x_n separated by spaces, describing the songs that were played (1≤xi≤s1 \le x_i \le s).

Output

For each test case, print on a single line the number of ways to split the record into valid blocks (the number of possible positions for the first block boundary). If no valid split exists, print 00.

Examples5

  1. Example 1

    Input
    4
    4 10
    3 4 4 1 3 2 1 2 3 4
    6 6
    6 5 4 3 2 1
    3 5
    3 3 1 1 1
    7 3
    5 7 3
    
    Expected output
    1
    6
    0
    7
    
  2. Example 2

    Input
    5
    1 1
    1
    1 6
    1 1 1 1 1 1
    5 1
    3
    5 5
    5 4 3 2 1
    2 3
    1 1 1
    
    Expected output
    1
    1
    5
    5
    0
    
  3. Example 3

    Input
    3
    10 4
    7 2 9 4
    5 2
    3 3
    7 7
    1 2 3 4 5 6 7
    
    Expected output
    10
    1
    7
    
  4. Example 4

    Input
    4
    100000 1
    50000
    2 2
    1 2
    2 4
    1 2 1 2
    3 3
    2 2 2
    
    Expected output
    100000
    2
    2
    0
    
  5. Example 5

    Input
    3
    6 3
    2 4 6
    8 8
    8 7 6 5 4 3 2 1
    4 6
    1 2 3 4 1 2
    
    Expected output
    6
    8
    4