P18103 [CCPC 2026 Changchun] Residual Mirror

Description

While tidying up a folding mirror made of $n$ mirror pieces, Little R finds that after taking the sequence $a_1,\dots,a_n$ modulo an integer $m\ge 2$, some intervals turn into palindromes. For positive integers $l,r,m$ ($1\le l\le r\le n$), if $[a_l\bmod m,\ a_{l+1}\bmod m,\ \dots,\ a_r\bmod m]$ is a palindromic sequence (that is, $(a_i \bmod m)=(a_{r+l-i}\bmod m)$ holds for every $l\le i \le r$), define $f(l,r,m)=1$; otherwise define $f(l,r,m)=0$. Given the upper bound $M$, please compute: $$ \sum_{1\le l

Input Format

The first line contains two integers $n,M$ ($1\le n \le 8000$, $2\le M \le 10^6$), representing the length of the sequence and the upper bound of the modulus. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_i \le M$), representing the sequence $a$.

Output Format

Output a single line with one integer, which is the required answer $\sum\limits_{1\le l

Explanation/Hint

In total there are the following $3$ triples $(l,r,m)$ with $f(l,r,m)=1$: - $(l,r,m)=(1,2,2)$: here $[a_1\bmod 2,\ a_2\bmod 2]=[1,1]$ is a palindromic sequence. - $(l,r,m)=(3,4,2)$: here $[a_3\bmod 2,\ a_4\bmod 2]=[0,0]$ is a palindromic sequence. - $(l,r,m)=(2,5,2)$: here $[a_2\bmod 2,\ a_3\bmod 2,\ a_4\bmod 2,\ a_5\bmod 2]=[1,0,0,1]$ is a palindromic sequence. For $(l,r,m)=(2,5,3)$, $[a_2\bmod 3,\ a_3\bmod 3,\ a_4\bmod 3,\ a_5\bmod 3]=[0,2,1,0]$ is not a palindromic sequence, so $f(2,5,3)=0$. Translated by QwenWork