Under construction

Still being built. Things may change

Unicorn

Time limit2sMemory limit128 MB

Summary
Count the number of paths on an N by M letter grid where a chess unicorn piece spells out a given word, modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Matrix, Combinatorics
Solved
No attempts yet

Problem

A unicorn is a chess piece similar to a knight. A knight moves two squares in one direction and one square in a perpendicular direction. A unicorn instead moves more than two squares in one of the four cardinal directions, then more than one square in one of the two perpendicular directions.

More precisely, one move is performed as follows.

  1. Pick up the unicorn.
  2. Move it at least 33 squares in one of the four cardinal directions.
  3. Move it at least 22 squares in one of the two directions perpendicular to the direction just used.
  4. Put the unicorn down.

The chessboard has size N×MN \times M. Each square contains one of the first LL uppercase English letters.

Given NN, MM, LL, and a word, count the number of unicorn paths whose occupied squares spell the given word in order. Print the count modulo 1,000,000,0071{,}000{,}000{,}007.

Input

The first line contains NN, MM, and LL. Both NN and MM are positive integers no greater than 300300, and LL is a positive integer no greater than 2626.

The second line contains the word. Its length is at most 5050, and it consists only of uppercase English letters.

From the third line, NN lines follow. Each of these lines is a string of length MM describing the letters written on the chessboard.

Output

Print the number of paths modulo 1,000,000,0071{,}000{,}000{,}007 on the first line.

Examples7

  1. Example 1

    Input
    3 4 2
    AB
    ABBA
    AAAA
    BBBB
    
    Expected output
    2
    
  2. Example 2

    Input
    5 5 2
    CD
    ABBAA
    AAABB
    BBBBB
    ABABA
    ABBBB
    
    Expected output
    0
    
  3. Example 3

    Input
    4 4 1
    AA
    AAAA
    AAAA
    AAAA
    AAAA
    
    Expected output
    20
    
  4. Example 4

    Input
    4 4 1
    AAAAA
    AAAA
    AAAA
    AAAA
    AAAA
    
    Expected output
    172
    
  5. Example 5

    Input
    1 1 5
    ABCDE
    C
    
    Expected output
    0
    
  6. Example 6

    Input
    8 8 26
    TOPCODER
    AILFPSPF
    DZIOMYCE
    QOODZARU
    YVOTLTRX
    LSRIGANL
    LCIUUSNF
    IWVXKTDE
    OVPPNXRD
    
    Expected output
    1
    
  7. Example 7

    Input
    20 20 2
    AAAAA
    ABBAAAAABBBBBBBABABA
    ABBBBBABAAAABBAAABAA
    BAAABAABAABBABABBABB
    BBABBAAAABABAAAAABBA
    BBABAABABBAABABABBBA
    BABABAABABBBABBAABBA
    BAABBAAABBBABBABAAAA
    BAABBBBABAABAAAAABAA
    AABABAAAAABBBABABBBA
    BBAABAAABBAABAAAAABA
    BAAAAABABBAAABABABBA
    ABBAABBBABABBABAAABB
    AAAABAAAAAAAABBBAABB
    AAABABAAAAAABAAABABB
    AAABABABBABABAAABBBA
    AAABBBBAABBAABAAABBA
    BBBBAAABBABABAAAABAB
    BBAABBAABAAAABAABABA
    BBBAABBABABBBBBBBBBA
    AAABABBBBAABABBBBBBB
    
    Expected output
    373977054