Split the GSHS 2
InterviewTime limit1sMemory limit1024 MB
Count the ways to split students 1 to N into consecutive groups, where each group's size is an endpoint leader's A plus 1, modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Gyeonggi Science High School, a landmark of Gyeonggi-do, has students. The villain Donghyun plans to divide the students of Gyeonggi Science High School and then take control of the school. The first part of his plan is to weaken the school by splitting its students into several groups. Donghyun intends to form the groups according to the following rules.
- Each student has a unique leadership value . A student with leadership can be the leader of a group that contains exactly members other than themselves. In other words, the size of a group led by the student with leadership must be exactly , including the leader.
- Every student must belong to exactly one group. No student may belong to no group or to two or more groups.
- Every group must consist of students with consecutive numbers. That is, the student numbers in a group must have the form for some .
- For the group to be represented fairly, the leader must be either the student with the smallest number or the student with the largest number. That is, in a group whose student numbers form , the leader is student or student .
The problem is that there are a very large number of ways to split the groups. Donghyun asks you to count the ways to split the students. Write a program that computes this number for him.
Input
The first line contains the number of students at Gyeonggi Science High School.
The second line contains integers , , , , separated by spaces, which represent the leadership of each student.
Output
Print, on the first line, the number of ways to split the students according to the rules above, modulo .