Farmer John's farm is an N x N grid of fields (1 <= N <= 500), each labeled with a letter A..Z.
Each day, Bessie walks from the upper-left field to the lower-right field, each step taking her either one field to the right or one field downward. She keeps track of the string she generates, and gets disoriented if this string is a palindrome.
Please help Bessie determine the number of distinct routes she can take that correspond to palindromes. Different ways of obtaining the same palindrome count multiple times. Print your answer modulo 1,000,000,007.
Input
Line 1: N. The remaining N lines contain the N rows of the grid; each row contains N characters in the range A..Z.
Output
The number of distinct palindromic routes Bessie can take, modulo 1,000,000,007.
Example 1
4 ABCD BXZX CDXB WCBA
12
Output details: 1x ABCDCBA, 1x ABCWCBA, 6x ABXZXBA, 4x ABXDXBA = 12 routes.
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.