Under construction

Still being built. Things may change

Pilgrim's Memories and the Impressed God

Time limit8sMemory limit1024 MB

Summary
Given an N by N grid of 0 and 1 trials, sum X^2+1 over every distinct color string formed by monotone chains of sites, where X counts how often that string occurs.
Level

Hard9 of 10

Topics
Dynamic programming, String, Combinatorics
Solved
No attempts yet

Problem

A pilgrim has been on a pilgrimage that seems like it will never end. There are N2N^2 holy sites on the earth. They are laid out in an N×NN \times N grid, and the site in row ii from the top and column jj from the left is called site (i,j)(i, j). A pilgrim who arrives at a site always receives either a black trial or a white trial. A black trial is a "trial of karma" that pays off one's sins, and a white trial is a "trial for ascent" that raises one's rank.

The pilgrim starts at site (1,1)(1, 1), called "the mortal world," and receives a trial there. After a trial at site (i,j)(i, j), the pilgrim moves to site (i+1,j)(i+1, j) or (i,j+1)(i, j+1) and receives another trial. This repeats until the pilgrim reaches site (N,N)(N, N), called "liberation," and receives the final trial. One pilgrimage visits 2N−12N-1 sites in total.

In this way, there are (2N−2N−1)\binom{2N-2}{N-1} different pilgrimages. To reach enlightenment, the pilgrim carried out every pilgrimage exactly once.

After all the pilgrimages, the resting pilgrim recalls them. A memory is a list of one or more sites in an order the pilgrim could have moved during a pilgrimage. Precisely, a memory of ll sites (i1,j1),(i2,j2),⋯ ,(il,jl)(i_1,j_1), (i_2,j_2), \cdots, (i_l,j_l) must satisfy all of the following: i1≤i2≤⋯≤ili_1 \le i_2 \le \cdots \le i_l, j1≤j2≤⋯≤jlj_1 \le j_2 \le \cdots \le j_l, and (ik−1,jk−1)≠(ik,jk)(i_{k-1},j_{k-1}) \neq (i_k,j_k) for every 2≤k≤l2 \le k \le l.

For example, when N=2N=2, the pilgrim can recall 4 + 5 + 2 = 11 memories in total.

The benevolent and omnipotent god watched all of this and was deeply impressed. The god cares more about which trial was received than where it was received, so the god reads every site in the memories as the trial given there. After seeing all the memories, the god is impressed by X2+1X^2+1 for each memory that appears at least once, where XX is the number of times that memory appears. Find the total amount the god is impressed by.

Input

The first line contains a natural number NN, the size of the grid.

Each of the next NN lines contains a string of length NN made only of 0 and 1. The jj-th character of the ii-th line is 0 if site (i,j)(i, j) gives a black trial, and 1 if it gives a white trial.

Output

Print, on the first line, the total impression of the god modulo 109+710^9+7.

Examples3

  1. Example 1

    Input
    2
    00
    00
    
    Expected output
    48
    
  2. Example 2

    Input
    2
    00
    10
    
    Expected output
    30
    
  3. Example 3

    Input
    5
    11010
    00110
    10110
    10110
    10111
    
    Expected output
    1304460