atcoder#ARC158E. [ARC158E] All Pair Shortest Paths

[ARC158E] All Pair Shortest Paths

Score : 800800 points

Problem Statement

We have a grid with 22 rows and NN columns. Let (i,j)(i,j) denote the square at the ii-th row from the top and jj-th column from the left. (i,j)(i,j) has a postive integer xi,jx_{i,j} written on it.

Two squares are said to be adjacent when they share a side.

A path from square XX to YY is a sequence (P1,,Pn)(P_1, \ldots, P_n) of distinct squares such that P1=XP_1 = X, Pn=YP_n = Y, and PiP_i and Pi+1P_{i+1} are adjacent for every 1in11\leq i \leq n-1. Additionally, the weight of that path is the sum of integers written on P1,,PnP_1, \ldots, P_n.

For two squares XX and YY, let f(X,Y)f(X, Y) denote the minimum weight of a path from XX to YY. Find the sum of f(X,Y)f(X, Y) over all pairs of squares (X,Y)(X,Y), modulo 998244353998244353.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1xi,j1091\leq x_{i,j} \leq 10^9

Input

The input is given from Standard Input in the following format:

NN

x1,1x_{1,1} \ldots x1,Nx_{1,N}

x2,1x_{2,1} \ldots x2,Nx_{2,N}

Output

Print the sum of f(X,Y)f(X, Y) over all pairs of squares (X,Y)(X,Y), modulo 998244353998244353.

1
3
5
24

You should find the sum of the following four values.

  • For X=(1,1),Y=(1,1)X = (1,1), Y = (1,1): f(X,Y)=3f(X, Y) = 3.
  • For X=(1,1),Y=(2,1)X = (1,1), Y = (2,1): f(X,Y)=8f(X, Y) = 8.
  • For X=(2,1),Y=(1,1)X = (2,1), Y = (1,1): f(X,Y)=8f(X, Y) = 8.
  • For X=(2,1),Y=(2,1)X = (2,1), Y = (2,1): f(X,Y)=5f(X, Y) = 5.
2
1 2
3 4
76
5
1 1000000000 1 1 1
1 1 1 1000000000 1
66714886