#P6693. 谷歌翻(sheng)译(cao)机

谷歌翻(sheng)译(cao)机

Background

Little L has recently been obsessed with using Google’s “shengcao” generator to create all kinds of weird stuff.

After producing many different works, Little L began to think about the following problem.

Problem Description

Note: For convenience, in the following, all strings are 1-indexed, i.e., positions start from 11.

Little L treats the original text before each “shengcao” and the resulting text after “shengcao” as two strings AA and BB, both consisting only of lowercase letters.

We define a “partition sequence” and “partition substrings” as follows:

  • For a string of length nn, a “partition sequence” is defined as: there exists a sequence pp of length k+2k+2 such that 0=p0<p1<p2<...<pk<pk+1=n+10=p_0<p_1<p_2<...<p_k<p_{k+1}=n+1. For a partition sequence, its “partition substrings” are the substrings formed by the characters from position pi+1p_i+1 to pi+11p_{i+1}-1 (i[0,k]i \in[0,k], possibly the empty string). Clearly, for a partition sequence of length k+2k+2, there are k+1k+1 partition substrings in total.

  • For the same string, two partition sequences (pp and qq) are different if and only if their lengths are different (k1k2k_1\neq k_2), or there exists an ii such that piqip_i\neq q_i.

Different people may understand the same original text and result in different ways. We define one way of understanding as follows:

  • For strings AA and BB, we choose one partition sequence for each of them (pp and qq). These two partition sequences must satisfy:
  1. The two partition sequences have the same length (k1=k2k_1=k_2).
  2. For any ii, we have A[pi]=B[qi]A[p_i]=B[q_i], i.e., the character at position pip_i in AA is the same as the character at position qiq_i in BB.
  • Define the “shengcao level” of this way of understanding as the sum of squares of the lengths of all partition substrings of the two strings, i.e., $\sum\limits_{i=0}^{k_1}(p_{i+1}-p_i-1)^2+\sum\limits_{i=0}^{k_2}(q_{i+1}-q_i-1)^2$.

  • Two ways of understanding are different if and only if their pp are different, or their qq are different.

Little L wants to know the sum of the shengcao levels over all ways of understanding. Since he does not like the number 109+710^9+7, he does not want you to tell him the result as that number, so you need to take the result modulo 109+710^9+7.

Input Format

The first line contains two positive integers n,mn,m, which are the lengths of AA and BB.

The next line contains a string of length nn, representing string AA.

The next line contains a string of length mm, representing string BB.

Output Format

One line containing one integer, the answer modulo 109+710^9+7.

3 4
abc
bacb

74
7 9
adcbbde
bdaegbcba

2128

Hint

For sample 1, there are the following ways of understanding:

  • p={0,4},q={0,5}p=\{0,4\},q=\{0,5\}, the shengcao level is 2525.
  • p={0,1,4},q={0,2,5}p=\{0,1,4\},q=\{0,2,5\}, the shengcao level is 99.
  • p={0,2,4},q={0,1,5}p=\{0,2,4\},q=\{0,1,5\}, the shengcao level is 1111.
  • p={0,2,4},q={0,4,5}p=\{0,2,4\},q=\{0,4,5\}, the shengcao level is 1111.
  • p={0,3,4},q={0,3,5}p=\{0,3,4\},q=\{0,3,5\}, the shengcao level is 99.
  • p={0,1,2,4},q={0,2,4,5}p=\{0,1,2,4\},q=\{0,2,4,5\}, the shengcao level is 33.
  • p={0,1,3,4},q={0,2,3,5}p=\{0,1,3,4\},q=\{0,2,3,5\}, the shengcao level is 33.
  • p={0,2,3,4},q={0,1,3,5}p=\{0,2,3,4\},q=\{0,1,3,5\}, the shengcao level is 33.

The total shengcao level is 7474.

Constraints

This problem uses bundled testdata.

  • Subtask 1( 20%20\% ): n,m50n,m\leq 50.
  • Subtask 2( 30%30\% ): n,m200n,m\leq 200.
  • Subtask 3( 50%50\% ): no special constraints.

For 100%100\% of the data, n,m3000n,m\leq 3000, and AA and BB contain only lowercase letters.

Translated by ChatGPT 5