Variable Subsequences
Time limit2sMemory limit512 MB
Count distinct nonempty subsequences, identified by position sets, in which every two consecutive terms differ.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Array
- Solved
- No attempts yet
Problem
We are looking for variable subsequences of a given sequence . A subsequence is obtained by removing any number of terms (possibly none). Formally, a subsequence of is any sequence with .
A variable subsequence is a subsequence in which every two consecutive terms are different. For example, is a variable subsequence of .
We want to count how many distinct, nonempty variable subsequences the sequence has. Two subsequences are distinct if their sets of positions in are different. For instance, contains two distinct variable subsequences of the form .
Input
The first line contains one integer (), the length of the sequence . The second line contains integers () separated by spaces.
Output
Print a single integer on one line: the number of nonempty variable subsequences of the input sequence modulo .
Hint
For the sequence , the counted variable subsequences are:
- : counted three times (positions 1, 3, and 4)
- : counted once
- : counted once
- : counted twice
- : counted twice
In total there are of them.