Sequences, Queries, and Probability 3
Time limit1.5sMemory limit1024 MB
Given an array and M random operations, find the expected ratio of the final sum (S) or product (P) to the initial one, modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Probability, Math, Number theory, Prefix sum
- Solved
- No attempts yet
Problem
While running a joint seminar, we noticed that most of the advanced class topics in Semester 1 were problems built around sequence and query or tree and query exercises. Jaewoo covered sqrt decomposition and Mo's algorithm in Week 1, Jinhan covered HLD and dynamic tree DP in Week 6, and Jongwoo covered Segment Tree Beats and PST in Weeks 7 and 8. All of them were given sequence and query or tree and query problems as exercises. Dongwoo took this chance to ask Jinhan questions and started solving the sequence and query series.
While working through the series, Dongwoo wondered why sequence and query problems are never given with probabilities. So he made the following problem.
Dongwoo wants to apply the operation queries below a total of times to a sequence of length .
1: Applies to . ()2: Applies to for every . ()3: Applies to every for every . ()4: Applies for every . ()
Dongwoo decides each of the queries independently by the following rule.
- Out of the possible queries (N of type 1, and each of types 2, 3, and 4), one query is applied with equal probability. Note that if , the type 1 query always applies.
Dongwoo wants to run one of the two question queries below.
S: Let be the sum of the elements in the initial state and the sum in the final state. Find the value of .P: Let be the product of the elements in the initial state and the product in the final state. Find the value of .
Impatient Dongwoo wants to know the expected value of the answer to the question query before processing any queries. Help Dongwoo find it.
Input
The first line contains the number of elements and the number of operation queries , separated by a space.
The second line contains the sequence in its initial state, which consists of positive integers separated by spaces.
The third line contains the question query character that Dongwoo asked about. is either S or P.
Output
Print the expected value of the answer to the question query modulo on the first line.
For an irreducible fraction , its value modulo is defined as , where is the integer satisfying , that is, the multiplicative inverse of modulo . If the value is an integer, then , so the result is .
Under the given conditions, the expected value can be proven to be an integer or a rational number. For a rational number, the denominator of the irreducible fraction is guaranteed not to be a multiple of .