Under construction

Still being built. Things may change

Sequences, Queries, and Probability 3

Time limit1.5sMemory limit1024 MB

Summary
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 MM times to a sequence A=A1,A2,⋯ ,ANA=A_1,A_2,\cdots,A_N of length NN.

  • 1 ii: Applies Ai=Ai×iA_i=A_i\times i to AiA_i. (1≤i≤N1\le i\le N)
  • 2 ll rr: Applies Ai=Ai×iA_i=A_i\times i to AiA_i for every l≤i≤rl\le i\le r. (1≤l<r≤N1\le l<r\le N)
  • 3 ll rr: Applies Ai=Ai×lA_i=A_i\times l to every AiA_i for every l≤i≤rl\le i\le r. (1≤l<r≤N1\le l<r\le N)
  • 4 ll rr: Applies Ai=Ai×rA_i=A_i\times r for every l≤i≤rl\le i\le r. (1≤l<r≤N1\le l<r\le N)

Dongwoo decides each of the MM queries independently by the following rule.

  • Out of the 3(N2)+N3\binom{N}{2}+N possible queries (N of type 1, and (N2)\binom{N}{2} each of types 2, 3, and 4), one query is applied with equal probability. Note that if N=1N=1, the type 1 query always applies.

Dongwoo wants to run one of the two question queries below.

  • S: Let S0S_0 be the sum of the elements in the initial state and S1S_1 the sum in the final state. Find the value of S1S0\frac{S_1}{S_0}.
  • P: Let P0P_0 be the product of the elements in the initial state and P1P_1 the product in the final state. Find the value of P1P0\frac{P_1}{P_0}.

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 N(1≤N≤106)N(1\le N\le 10^6) and the number of operation queries M(1≤M≤1018)M(1\le M\le 10^{18}), separated by a space.

The second line contains the sequence A1,A2,⋯ ,AN(1≤Ai≤109)A_1,A_2,\cdots,A_N(1\le A_i\le 10^9) in its initial state, which consists of positive integers separated by spaces.

The third line contains the question query character QQ that Dongwoo asked about. QQ is either S or P.

Output

Print the expected value of the answer to the question query modulo 109+710^9+7 on the first line.

For an irreducible fraction pq(p≥0,q>0,gcd⁡(p,q)=1)\frac{p}{q}(p\ge 0,q>0,\gcd(p,q)=1), its value modulo 109+710^9+7 is defined as p⋅q−1(mod109+7)p\cdot q^{-1}\pmod{10^9+7}, where q−1q^{-1} is the integer satisfying q⋅q−1≡1(mod109+7)q\cdot q^{-1}\equiv 1\pmod{10^9+7}, that is, the multiplicative inverse of qq modulo 109+710^9+7. If the value is an integer, then q=q−1=1q=q^{-1}=1, so the result is p(mod109+7)p\pmod{10^9+7}.

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 109+710^9+7.

Examples4

  1. Example 1

    Input
    3 1
    3 2 1
    S
    
    Expected output
    555555561
    
  2. Example 2

    Input
    3 1
    3 2 1
    P
    
    Expected output
    500000009
    
  3. Example 3

    Input
    3 111111111666666666
    3 2 1
    S
    
    Expected output
    1
    
  4. Example 4

    Input
    3 999999999999999964
    3 2 1
    P
    
    Expected output
    1