Under construction

Still being built. Things may change

Secret of the Sequence (Hard)

Interview

Time limit1sMemory limit1024 MB

Summary
Given a sequence where a_2i and a_2i+1 are linear functions of a_i, find the sum of the first 2^k-1 terms modulo 1e9+7.
Level

Medium5 of 10

Topics
Divide and conquer, Recursion, Math
Solved
No attempts yet

Problem

There is a sequence a_1,a_2,a_3,…,a_na\_1, a\_2, a\_3, \ldots, a\_n of length nn that satisfies the following conditions.

  • a_2i=p⋅a_i+qa\_{2i} = p \cdot a\_i + q (2≤2i≤n)(2 \leq 2i \leq n)
  • a_2i+1=r⋅a_i+sa\_{2i+1} = r \cdot a\_{i} + s (3≤2i+1≤n)(3 \leq 2i+1 \leq n)

Let S_n=a_1+a_2+…+a_nS\_n=a\_1+a\_2+\ldots +a\_{n}. Print the remainder of S_nS\_n divided by 109+7{10^9+7}.

Input

The first line contains a positive integer kk. (n=2k−1)(n=2^k-1)

The second line contains positive integers p,q,r,sp,q,r,s, separated by spaces.

The third line contains a positive integer a_1a\_1.

Output

Print the remainder of S_nS\_n divided by 109+7{10^9 + 7} on the first line.

Constraints

  • 1≤k≤501\leq k\leq 50
  • 1≤p,q,r,s≤1001\leq p,q,r,s\leq 100
  • 1≤a_1≤101\leq a\_1\leq 10

Hint

Intermediate values can exceed the range of the C/C++ int type, so using long long is recommended.

Examples1

  1. Example 1

    Input
    3
    2 1 1 2
    1
    
    Expected output
    31