Kaavi and Magic Spell
Contest: Codeforces Round 635 (Div. 1)
Difficulty rating: 2200
Tags: dp, strings

Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future.

Kaavi has a string $$$T$$$ of length $$$m$$$ and all the strings with the prefix $$$T$$$ are magic spells. Kaavi also has a string $$$S$$$ of length $$$n$$$ and an empty string $$$A$$$.

During the divination, Kaavi needs to perform a sequence of operations. There are two different operations:

- Delete the first character of $$$S$$$ and add it at the front of $$$A$$$.
- Delete the first character of $$$S$$$ and add it at the back of $$$A$$$.

Kaavi can perform no more than $$$n$$$ operations. To finish the divination, she wants to know the number of different operation sequences to make $$$A$$$ a magic spell (i.e. with the prefix $$$T$$$). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo $$$998\,244\,353$$$.

Two operation sequences are considered different if they are different in length or there exists an $$$i$$$ that their $$$i$$$-th operation is different.

A substring is a contiguous sequence of characters within a string. A prefix of a string $$$S$$$ is a substring of $$$S$$$ that occurs at the beginning of $$$S$$$.

Input format:
The first line contains a string $$$S$$$ of length $$$n$$$ ($$$1 \leq n \leq 3000$$$).

The second line contains a string $$$T$$$ of length $$$m$$$ ($$$1 \leq m \leq n$$$).

Both strings contain only lowercase Latin letters.

Output format:
The output contains only one integer  — the answer modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
The first test:

The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of $$$A$$$, although the results are the same, they are considered as different operations. So the answer is $$$6\times2=12$$$.

Example input:
abab
ba

Example output:
12

Example input:
defineintlonglong
signedmain

Example output:
0

Editorial:
We can use DP to solve this problem.
Let $$$f_{i,j}$$$ be the answer when $$$S[0..i-1]$$$ has already been used and the current $$$A[0..\min(i-1,m-1-j)]$$$ will be in the position $$$[j..\min(i+j-1,m-1)]$$$ after all operations are finished. Specially, $$$f_{i,m}$$$ means that all characters in the current $$$A$$$ won't be in the position $$$[j..\min(i+j-1,m-1)]$$$ after all operations are finished.
By definition, $$$f_{n,0}=1$$$ and $$$f_{n, j}=0 (j\ge 1)$$$.
The state transition:
• $$$j=0$$$If $$$i\ge m$$$, $$$A$$$ has already had the prefix $$$T$$$, so you can stop performing operations at any position, there are $$$n-i+1$$$ positions in total.Otherwise, only when $$$S[i]=T[i]$$$, you can add $$$S[i]$$$ at the back of $$$A$$$, there are $$$f_{i+1,0}$$$ different operation sequences in this situation.
• $$$1\le j \le m-1$$$If $$$i+j\ge m$$$ or $$$S[i]=T[i+j]$$$, you can add $$$S[i]$$$ at the back of $$$A$$$, there are $$$f_{i+1,j}$$$ different operation sequences in this situation.If $$$S[i]=T[j-1]$$$, you can add $$$S[i]$$$ at the front of $$$A$$$, there are $$$f_{i+1,j-1}$$$ different operation sequences in this situation.
• $$$j=m$$$You can always add $$$S[i]$$$ at the front/back of $$$A$$$ in this situation ($$$2f_{i+1,m}$$$ different operation sequences). However, if $$$S[i]=T[m-1]$$$, you can let $$$S[i]$$$ to match $$$T[m-1]$$$ ($$$f_{i+1,m-1}$$$ different operation sequences).So, if $$$S[i]=T[m-1]$$$, $$$f_{i,m}=2f_{i+1,m}+f_{i+1,m-1}$$$. Otherwise, $$$f_{i,m}=2f_{i+1,m}$$$.
The answer is $$$2(f_{1,m}+\sum\limits_{T[i]=S[0]}f_{1,i})$$$.