#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
int main() {
int n;
scanf("%d",&n);
int mod = 1e9+7;
int a;
long b=0;
int cur, prevsize;
int build=0;
int *table[1000001L] = {NULL};
getchar();
while(b<(n*n)) {
cur = getchar();
if(cur==' '||cur=='\n') {
if(table[build]) {
prevsize = table[build][0];
table[build]=realloc(table[build],sizeof(int)*(prevsize+2));
if(!table[build]) printf("error with realloc");
table[build][0]=prevsize+1;
table[build][prevsize+1]=b;
} else {
table[build]=malloc(sizeof(int)*2);
table[build][0]=1;
table[build][1]=b;
}
b++;
build=0;
} else {
build*=10;
build+=cur-'0';
}
}
unsigned long finalcount = 0;
unsigned long xpaths[1000] = {0}, ypaths[1000] = {0};
unsigned long *newpaths;
unsigned long i, pathc;
unsigned long j, k, index;
short x, y;
for(i=0;i<=1000000L;i++) {
if(table[i]) {
k=table[i][0];
newpaths = malloc(sizeof(long)*k*3);
// printf("kana %d\n",table[i][k]);
for(j=0;j<k;j++) {
index = table[i][j+1];
x = index%n;
y = index/n;
pathc = (1+xpaths[x]+ypaths[y])%mod;
newpaths[j*3]=pathc;
newpaths[j*3+1]=x;
newpaths[j*3+2]=y;
finalcount=(finalcount+pathc)%mod;
}
for(j=0;j<k;j++) {
pathc=newpaths[j*3];
xpaths[newpaths[j*3+1]]+=pathc;
ypaths[newpaths[j*3+2]]+=pathc;
}
free(newpaths);
}
}
// printf("%u\n",UINT_MAX);
// for(i=0;i<1000000;i++) {
// finalcount=(finalcount+paths[i])%mod;
// }
printf("%lu\n",finalcount);
}