Mao-Kong Gondola is a famous attraction in Taipei. The gondola system consists of a circular rail, a single station, and gondolas numbered consecutively from 1 to running around the rail in a fixed direction. After gondola passes the station, the next gondola to pass the station will be gondola if , or gondola 1 if .

Gondolas may break down. Luckily we have an infinite supply of spare gondolas, which are numbered , , and so on. When a gondola breaks down we replace it (in the same position on the track) with the first available spare gondola, that is, the one with the lowest number. For example, if there are five gondolas and gondola 1 breaks down, then we will replace it with gondola 6.

You like to stand at the station and watch the gondolas as they pass by. A *gondola sequence* is a sequence of numbers of gondolas that pass the station. It is possible that one or more gondolas broke down (and were replaced) before you arrived, but none of the gondolas break down while you are watching.

Note that the same configuration of gondolas on the rail can give multiple gondola sequences, depending on which gondola passes first when you arrive at the station. For example, if none of the gondolas have broken down then both and are possible gondola sequences, but is not (because the gondolas appear in the wrong order).

If gondola 1 breaks down, then we might now observe the gondola sequence . If gondola 4 breaks down next, we replace it with gondola 7 and we might observe the gondola sequence . If gondola 7 breaks down after this, we replace it with gondola 8 and we may now observe the gondola sequence .

broken gondola | new gondola | possible gondola sequence |
---|---|---|

1 | 6 | |

4 | 7 | |

7 | 8 |

A *replacement sequence* is a sequence consisting of the numbers of the gondolas that have broken down, in the order in which they break down. In the previous example the replacement sequence is . A replacement sequence *produces* a gondola sequence if, after gondolas break down according to the replacement sequence , the gondola sequence may be observed.

#### Gondola Sequence Checking

In the first three subtasks you must check whether an input sequence is a gondola sequence. See the table below for examples of sequences that are and are not gondola sequences. You need to implement a function `valid`

.

`valid(n, inputSeq)`

`n`

: the length of the input sequence.`inputSeq`

: array of length ;`inputSeq[i]`

is element of the input sequence, for .- The function should return 1 if the input sequence is a gondola sequence, or 0 otherwise.

##### Subtasks 1, 2, 3

subtask | points | `inputSeq` | |
---|---|---|---|

1 | 5 | has each number from 1 to exactly once | |

2 | 5 | ||

3 | 10 |

##### Examples

subtask | `inputSeq` |
return value | note |
---|---|---|---|

1 | 1 | ||

1 | 1 | ||

1 | 0 | 1 cannot appear just before 5 | |

1 | 0 | 4 cannot appear just before 3 | |

2 | 0 | two gondolas numbered 5 | |

3 | 1 | replacement sequence | |

3 | 0 | 4 cannot appear just before 3 |

#### Replacement Sequence

In the next three subtasks you must construct a possible replacement sequence that produces a given gondola sequence. Any such replacement sequence will be accepted. You need to implement a function `replacement`

.

`replacement(n, gondolaSeq, replacementSeq)`

`n`

is the length of the gondola sequence.`gondolaSeq`

: array of length ;`gondolaSeq`

is guaranteed to be a gondola sequence, and`gondolaSeq[i]`

is element of the sequence, for .- The function should return , the length of the replacement sequence.
`replacementSeq`

: array that is sufficiently large to store the replacement sequence; you should return your sequence by placing element of your replacement sequence into`replacementSeq[i]`

, for .

##### Subtasks 4, 5, 6

subtask | points | `gondolaSeq` | |
---|---|---|---|

4 | 5 | ||

5 | 10 | ||

6 | 20 |

##### Examples

subtask | `inputSeq` |
return value | `replacementSeq` |
---|---|---|---|

4 | 1 | ||

4 | 0 | ||

5 | 2 |

#### Count Replacement Sequences

In the next four subtasks you must count the number of possible replacement sequences that produce a given sequence (which may or may not be a gondola sequence), modulo . You need
to implement a function `countReplacement`

.

`countReplacement(n, inputSeq)`

`n`

: the length of the input sequence.`inputSeq`

: array of length ;`inputSeq[i]`

is element of the input sequence, for .- If the input sequence is a gondola sequence, then count the number of replacement sequences that produce this gondola sequence (which could be extremely large), and
*return this number modulo*. If the input sequence is not a gondola sequence, the function should return 0. If the input sequence is a gondola sequence but no gondolas broke down, the function should return 1.

##### Subtasks 7, 8, 9, 10

subtask | points | `inputSeq` | |
---|---|---|---|

7 | 5 | ||

8 | 15 | , and at least of the initial gondolas did not break down. | |

9 | 15 | ||

10 | 10 |

##### Examples

subtask | `inputSeq` |
return value | replacement sequence |
---|---|---|---|

7 | 2 | or | |

8 | 1 | ||

9 | 0 | `inputSeq` is not a gondola sequence | |

10 | 2 | or |

#### Input Specification

- Line : , the subtask number your program must solve .
- Line : , the length of the input sequence.
- Line : If is , , or , this line contains . Otherwise this line contains .

#### Output Specification

- If is , , or : the output should consist of a single integer, if the input sequence is a gondola sequence, or otherwise.
- If is , , or : the output should consist of the integer , the length of the replacement sequence, followed by integers on the same line representing the replacement sequence itself.
- If is , , , or : the only line of output should consist of a single integer, the number of replacement sequences producing the input sequence modulo if the input sequence is a gondola sequence, if the input sequence is not a gondola sequence, or if the input sequence is a gondola sequence but no gondolas broke down.

#### Sample Input 1

```
1
30
16 26 18 19 20 13 22 21 24 25 17 27 28 29 30 1 2 3 11 5 6 8 7 9 10 12 4 23 14 15
```

#### Sample Output 1

`0`

#### Sample Input 2

```
2
6
3 4 5 6 1 2
```

#### Sample Output 2

`1`

#### Sample Input 3

```
3
7
1 2 3 4 5 6 5
```

#### Sample Output 3

`0`

#### Sample Input 4

```
4
2
3 2
```

#### Sample Output 4

`1 1`

#### Sample Input 5

```
5
14
12 13 14 1 2 3 4 5 6 7 8 9 10 11
```

#### Sample Output 5

`0`

#### Sample Input 6

```
6
50
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 51 47 48 49 50 1 2 3
```

#### Sample Output 6

`1 46`

#### Sample Input 7

```
7
11
4 5 6 7 8 9 10 11 1 2 3
```

#### Sample Output 7

`1`

#### Sample Input 8

```
8
14
6 94 8 9 10 93 12 13 95 1 2 3 4 5
```

#### Sample Output 8

`224280120`

#### Sample Input 9

```
9
4
1 2 7 6
```

#### Sample Output 9

`2`

#### Sample Input 10

```
10
4
4 7 4 7
```

#### Sample Output 10

`0`

## Comments