Hide

Problem J
The King's Walk

/problems/kingswalk/file/statement/en/img-0001.jpg
Picture by: Michael Maggs

Chess is a game in which two sides control pieces in an attempt to capture each other’s king. The pieces vary in mobility. At the beginning of a game the kings are rather vulnerable. They are less mobile than most other pieces and they tend to hide behind their pawns. Like in real life, as soon as both queens have left the game it is time for the kings to come into action. Because there is little threat left for the king, he can now move safely around the board. Indeed his mobility seems to be quite strong at this stage making him one of the more dangerous pieces. Your task is to measure the mobility of the king in the endgame.

Consider a chess board of $N\times N$ squares. The king is the only piece on the board. He starts at a given position and wants to go to another given position in the minimum number of moves. The king can move to any adjacent square in any orthogonal or diagonal direction.

Input

The input starts with a line containing an integer $T$ ($1 \leq T \leq 100$), the number of test cases. Then for each test case:

  • One line with a single integer $N$, the size of the board, where $2\le N\le 5\, 000$.

  • One line with four space-separated integers $X_1, Y_1, X_2, Y_2$, such that $1\le X_1, Y_1, X_2, Y_2\le N$, where ($X_1,Y_1$) is the square on which the king starts and ($X_2,Y_2$) is the square the king wants to go to (different from his starting position).

Output

For each test case, output one line with a single integer: the number of ways by which the king can reach the destination square in the minimum number of moves. As this number can be very large, you must reduce your answer modulo $5\, 318\, 008$.

Sample Input 1 Sample Output 1
2
3
1 2 3 2
8
2 2 7 7
3
1

Please log in to submit a solution to this problem

Log in