#P5487. 【模板】Berlekamp–Massey 算法

【模板】Berlekamp–Massey 算法

Description

You are given the first nn terms of a sequence PP starting from index 00.

Find the shortest linear recurrence of sequence PP modulo 998244353998244353, and output PmP_m modulo 998244353998244353.

Input Format

The first line contains two integers n,mn,m, meaning that the first nn terms of sequence PP will be given, and you need to compute PmP_m.

The second line contains nn integers, which are P0,P1,P2,,Pn1P_0,P_1,P_2,\ldots,P_{n-1}.

Output Format

On the first line, output the shortest linear recurrence.

On the second line, output the value of PmP_m.

4 10
1 1 2 3

1 1 
89

5 10
3 7 27 95 339
3 2
691707

Hint

For 100%100\% of the testdata, n<m109n < m \le 10^9, 1n100001 \le n \le 10000, and the length of the recurrence is guaranteed to be at most 50005000.

Translated by ChatGPT 5