#P1464. Cyclic Substrings

    ID: 465 传统题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>省选/NOI-字符串数据结构回文自动机(PAM)

Cyclic Substrings

Cyclic Substrings

题目描述

Mr. Ham is interested in strings, especially palindromic strings. Today, he finds a string ss of length nn.

For the string ss of length nn, he defines its cyclic substring from the ii-th character to the jj-th character (1i,jn1\leq i,j\leq n) as follows:

  • If iji \leq j, the cyclic substring is the substring of ss from the ii-th character to the jj-th character. He denotes it as s[i..j]s[i..j].
  • If i>ji > j, the cyclic substring is s[i..n]+s[1..j]s[i..n] + s[1..j], where ++ denotes the concatenation of two strings. He also denotes it as s[i..j]s[i..j].

For example, if s=12345s = \texttt{12345}, then s[2..4]=234s[2..4] = \texttt{234}, s[4..2]=4512s[4..2] = \texttt{4512}, and s[3..3]=3s[3..3] = \texttt{3}.

A string tt is palindromic if t[i]=t[ni+1]t[i]=t[n-i+1] for all ii from 11 to nn. For example, 1221\texttt{1221} is palindromic, while 123\texttt{123} is not.

Given the string ss, there will be many cyclic substrings of ss which are palindromic. Denote PP as the set of all distinct cyclic substrings of ss which are palindromic, f(t)(tP)f(t)(t \in P) as the number of times tt appears in ss as a cyclic substring, and g(t)(tP)g(t)(t \in P) as the length of tt. Mr. Ham wants you to compute

tPf(t)2×g(t)\sum_{t \in P} f(t)^2 \times g(t)

The answer may be very large, so you only need to output the answer modulo 998244353998\,244\,353.

输入格式

The first line contains a number nn (1n3×1061 \leq n \leq 3\times 10^6), the length of the string ss.

The second line contains a string ss of length nn. Each character of ss is a digit.

输出格式

Output a single integer, denoting the sum modulo 998244353998\,244\,353.

样例 #1

样例输入 #1

5
01010

样例输出 #1

39

提示

Note

In the sample, the palindromic cyclic substrings of ss are:

  • s[1..1]=s[3..3]=s[5..5]=0s[1..1] = s[3..3] = s[5..5] = \texttt{0}.
  • s[2..2]=s[4..4]=1s[2..2] = s[4..4] = \texttt{1}.
  • s[5..1]=00s[5..1] = \texttt{00}.
  • s[1..3]=s[3..5]=010s[1..3] = s[3..5] = \texttt{010}.
  • s[2..4]=101s[2..4] = \texttt{101}.
  • s[4..2]=1001s[4..2] = \texttt{1001}.
  • s[1..5]=01010s[1..5] = \texttt{01010}.

The answer is $3^2 \times 1 + 2^2 \times 1 + 1^2 \times 2 + 2^2 \times 3 + 1^2 \times 3 + 1^2 \times 4 + 1^2 \times 5 = 39$.