This page is still under construction.

Parts of this page are still being built. What you see may change.

Palindrome Path 3

Time limit1sMemory limit256 MB

Summary
Count the paths from the top-left to the bottom-right corner moving only right or down whose letters form a palindrome, modulo 1000000007.
Level

Medium7 of 10

Topics
Dynamic programming, Matrix
Solved
No attempts yet

Problem

An N×NN \times N farm grid (1≤N≤5001 \leq N \leq 500) contains uppercase letters. A calf starts at (1,1)(1,1) and moves only right or down to reach (N,N)(N,N). It gets lost whenever the letters on its path form a palindrome. Count how many palindrome paths exist, modulo 1,000,000,0071{,}000{,}000{,}007.

Input

Line 1: NN. Next NN lines: the grid rows.

Output

Print the number of palindrome paths modulo 1,000,000,0071{,}000{,}000{,}007.

Note

On the sample 4×44 \times 4 grid the answer is 1212.

Examples4

  1. Example 1

    Input
    4
    ABCD
    BXZX
    CDXB
    WCBA
    
    Expected output
    12
    
  2. Example 2

    Input
    2
    AA
    AA
    
    Expected output
    2
    
  3. Example 3

    Input
    3
    ABC
    DEF
    GHI
    
    Expected output
    0
    
  4. Example 4

    Input
    2
    AB
    CD
    
    Expected output
    0