Problem list
Log in
#usaco0193 · USACO 2014-2015 US Open Gold

Palindromic Paths (Gold)

Gold III 2.0 s 256 MB

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

Input
4
ABCD
BXZX
CDXB
WCBA
Output
12

Output details: 1x ABCDCBA, 1x ABCWCBA, 6x ABXZXBA, 4x ABXDXBA = 12 routes.

Source
USACO 2014-2015 US Open Gold
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code