#P37. [KBC002D] String 1

[KBC002D] String 1

Source

This problem is adapted from Long Long OJ. All rights reserved.

Description

Given a letter matrix SS with 22 rows and nn columns.

Count the number of positions that meet the following condition: there is a path of length mm that starts from that position (each time you can go up/down/left/right by one position; passing the same position twice is allowed), where the letters on the mm positions passed through are T1,T2,,TmT_1,T_2,\ldots,T_m, respectively.

Take the answer modulo 10071007.

Constraints

  • 1N20001 \leq N \leq 2000 and 1M20001 \leq M \leq 2000.
  • The solution is guaranteed to exist.

Input

N MN\ M A1A2ANA_1A_2\cdots A_N B1B2BNB_1B_2\cdots B_N S1S2SMS_1S_2\cdots S_M

  • The first line contains two positive integers NN and MM.
  • The next two lines contain strings AA and BB.
  • The last line contains MM characters representing SS.

Output

The count of valid positions modulo 10071007.

Sample Input 1

4 4
LOVE
EVOL
LOVE

Sample Output 1

4
  • Possibility 1: (1,1)(1,2)(1,3)(1,4)(1,1) \to (1,2) \to (1,3) \to (1,4);
  • Possibility 2: (1,1)(1,2)(2,2)(2,1)(1,1) \to (1,2) \to (2,2) \to (2,1);
  • Possibility 3: (2,4)(2,3)(2,2)(2,1)(2,4) \to (2,3) \to (2,2) \to (2,1);
  • Possibility 4: (2,4)(2,3)(1,3)(1,4)(2,4) \to (2,3) \to (1,3) \to (1,4).

Sample Input 2

1 3
Y
Y
YYY

Sample Output 2

2
  • Possibility 1: (1,1)(1,2)(1,1)(1,1) \to (1,2) \to (1,1);
  • Possibility 2: (1,2)(1,1)(1,2)(1,2) \to (1,1) \to (1,2).