luogu#P5011. 水の造题

水の造题

Background

In the first minute, CYJian said, “There should be some form.” So there were kk kinds of moves.

In the second minute, CYJian said, “There should be energy.” So there was a combat-aerobics routine with a total of NN moves composed of the kk kinds of moves.

In the third minute, CYJian said, “There should be math.” So there was a definition of the power of a routine.

In the fourth minute, CYJian said, “Numbers should be small.” So there was a great modulus 1949100119491001.

In the fifth minute, CYJian said, “There should be a pattern.” So there was a way to compute the power of a routine.

In the sixth minute, CYJian said, “There should be limits.” So there were time and memory limits and Constraints.

In the seventh minute, CYJian said, “There should be an answer.” So there was this problem for you to solve.

……

In the *-th minute, the expert Imagine said, “The testdata is too easy.” So the newbie problem setter went crazy. (See Constraints for details.)

Problem Description

Now there is a combat-aerobics routine consisting of kk kinds of moves, with a total of NN moves.

It is known that the power of moves 1,2,,k1, 2, \cdots, k is a[1k]a[1\cdots k].

Also, if the first move is immediately followed by the second move, then the power will additionally increase by a[1]+a[2]a[1]+a[2].

If the second move is immediately followed by the third move, then the power will additionally increase by a[2]+a[3]a[2]+a[3].

……

If the kk-th move is immediately followed by the first move, then the power will additionally increase by a[k]+a[1]a[k]+a[1].

Please find the expected power of the final routine.

Of course, you still need to take it modulo the great modulus 1949100119491001.

Input Format

The first line contains a positive integer NN.

The second line contains a positive integer kk.

The third line contains kk positive integers a[1k]a[1\cdots k].

Output Format

One line, a positive integer representing the expected power modulo 1949100119491001.

2
6
1 2 3 4 5 6

16242509

Hint

Sample Explanation

xyx-y means the first move is xx and the second move is yy.

  • 111-1: 1+1=21+1=2;
  • 121-2: 1+2+(1+2)=61+2+(1+2)=6;
  • 131-3: 1+3=41+3=4;
  • 141-4: 1+4=51+4=5;
  • 151-5: 1+5=61+5=6;
  • 161-6: 1+6=71+6=7;
  • 212-1: 2+1=32+1=3;
  • 222-2: 2+2=42+2=4;
  • 232-3: 2+3+(2+3)=102+3+(2+3)=10;
  • 242-4: 2+4=62+4=6;
  • 252-5: 2+5=72+5=7;
  • 262-6: 2+6=82+6=8;
  • 313-1: 3+1=43+1=4;
  • 323-2: 3+2=53+2=5;
  • 333-3: 3+3=63+3=6;
  • 343-4: 3+4+(3+4)=143+4+(3+4)=14;
  • 353-5: 3+5=83+5=8;
  • 363-6: 3+6=93+6=9;
  • 414-1: 4+1=54+1=5;
  • 424-2: 4+2=64+2=6;
  • 434-3: 4+3=74+3=7;
  • 444-4: 4+4=84+4=8;
  • 454-5: 4+5+(4+5)=184+5+(4+5)=18;
  • 464-6: 4+6=104+6=10;
  • 515-1: 5+1=65+1=6;
  • 525-2: 5+2=75+2=7;
  • 535-3: 5+3=85+3=8;
  • 545-4: 5+4=95+4=9;
  • 555-5: 5+5=105+5=10;
  • 565-6: 5+6+(5+6)=225+6+(5+6)=22;
  • 616-1: 6+1+(6+1)=146+1+(6+1)=14;
  • 626-2: 6+2=86+2=8;
  • 636-3: 6+3=96+3=9;
  • 646-4: 6+4=106+4=10;
  • 656-5: 6+5=116+5=11;
  • 666-6: 6+6=126+6=12.

$2+6+4+5+6+7+3+4+10+6+7+8+4+5+6+14+8+9+5+6+7+8+18+10+6+7+8+9+10+22+14+8+9+10+11+12=294$.

294/36=49/6294/36=49/6.

1/616242501(mod19491001)1/6 \equiv 16242501 \pmod{19491001}.

ans=49×16242501mod19491001=16242509ans = 49 \times 16242501 \bmod 19491001 = 16242509.

Subtask 1 (20 pts): 1N101 \leq N \leq 10, 1k71 \leq k \leq 7.

  • Subtask 2 (20 pts): 1N1061 \leq N \leq 10^6, 1k71 \leq k \leq 7.
  • Subtask 3 (20 pts): 1N10400001 \leq N \leq 10^{40000}, 1k71 \leq k \leq 7.
  • Subtask 4 (20 pts): 1N101061 \leq N \leq 10^{10^6}, 1k71 \leq k \leq 7.
  • Subtask 5 (20 pts): 1N101061 \leq N \leq 10^{10^6}, 1k1061 \leq k \leq 10^6.

It is guaranteed for all testdata that 1a[i]1071 \leq a[i] \leq 10^7.

Note: This problem uses bundled tests.

A small hint:

You can use the following inv(x)\texttt{inv(}x\texttt{)} to compute the modular inverse of xx:

long long mod = 19491001;

long long quick_pow(long long x, int k) {
    long long res = 1;
    while(k) {
        if(k & 1) res = res * x % mod;
        x = x * x % mod;
        k >>= 1;
    }
    return res;
}

long long inv(long long x) {
    return quick_pow(x, mod - 2);
}

Translated by ChatGPT 5