Spiral Rectangle Sums

In a counterclockwise spiral of numbers on a (2n+1)x(2n+1) grid centered at 1, answer q queries for the sum inside an axis-aligned rectangle modulo 1e9+7.

Hard8MathImplementationPrefix sumNumber theoryNo attempts yetTime limit1.5sMemory limit256 MB

Problem

A grid of size (2n+1)×(2n+1)(2n+1) \times (2n+1) is filled as follows. Write 11 in the center square, write 22 in the square to its right, then keep writing the following numbers along a counterclockwise spiral.

A square is addressed by integer coordinates (x,y)(x, y). The center square is (0,0)(0, 0), xx grows to the right, and yy grows upward, so every square of the grid satisfies nxn-n \le x \le n and nyn-n \le y \le n. The grid for n=2n = 2 looks like this.

x=2x=-2x=1x=-1x=0x=0x=1x=1x=2x=2
y=2y=21716151413
y=1y=11854312
y=0y=01961211
y=1y=-12078910
y=2y=-22122232425

You are given qq rectangular regions. For each region, compute the sum of the numbers inside it modulo 109+710^9+7.

Input

The first line contains the size of the grid nn and the number of queries qq.

Each of the next qq lines contains four integers x1x_1, y1y_1, x2x_2, y2y_2. They describe the rectangular region with corners (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2), that is, every square with x1xx2x_1 \le x \le x_2 and y1yy2y_1 \le y \le y_2.

Output

For each query, print on its own line the sum of the numbers inside the region modulo 109+710^9+7.

Constraints

  • 1n1091 \le n \le 10^9
  • 1q1001 \le q \le 100
  • nx1x2n-n \le x_1 \le x_2 \le n
  • ny1y2n-n \le y_1 \le y_2 \le n