Under construction

Still being built. Things may change

Wizard Junkeun and the Magic Workshop

Time limit1sMemory limit1024 MB

Summary
Given N merge steps where each new stone is the sum of the two largest stones so far, print the last stone's value modulo 1e9+7.
Level

Medium5 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

Junkeun is a wizard who runs a small magic workshop. At his workshop, he combines several mana stones into better ones. One day he gets tired of the work and decides to build a machine that merges mana stones automatically.

The machine merges mana stones as follows.

  • Junkeun puts all of his mana stones into the machine.
  • The machine selects 22 of the stones it received.
  • It combines the two selected stones into a new mana stone. The mana value of the new stone is the sum of the mana values of the two ingredients. The ingredients are not consumed.
    • When it creates a new stone, it always creates the one with the largest mana value among the new stones it can make.
    • A stone created this way can be used as an ingredient later.
  • It returns all stones that were not used, all stones that were used, and the new stone as the result. Junkeun takes the result back.

Before starting the machine, Junkeun wants to know the mana value of the last stone created after repeating this process NN times. Given MM stones with mana values a1a_1, a2a_2, a3a_3, ⋯\cdots, aMa_M placed into the machine at the start, write a program that finds the mana value of the last stone created.

Input

The first line contains NN and MM, separated by a space.

The second line contains the integers a1a_1, a2a_2, a3a_3, ⋯\cdots, aMa_M, separated by spaces.

Output

Print the mana value of the last stone created. The absolute value can be very large, so express the value as p×q+rp \times q + r using three integers pp, qq, and rr, and print rr instead. (0≤r<p=109+70 \le r < p = 10^9+7)

Constraints

  • 1≤N≤1 000 0001 \le N \le 1\,000\,000
  • 2≤M≤100 0002 \le M \le 100\,000
  • −100 000≤ai≤100 000-100\,000 \le a_i \le 100\,000

Examples3

  1. Example 1

    Input
    1 2
    -1 -1
    
    Expected output
    1000000005
    
  2. Example 2

    Input
    2 3
    1 4 2
    
    Expected output
    10
    
  3. Example 3

    Input
    100 5
    1 6 3 0 4
    
    Expected output
    445205243