P7598 [APIO2021] Hexagonal Territory.
Background
This problem only supports C++ submissions. When submitting, you do not need to include the `hexagon.h` header file. You only need to paste the content of `hexagon.h` from the attachment at the beginning of your code.
Description
On a plane tiled infinitely with hexagons, Pak Dengklek stands on one cell and calls it the initial cell. If two cells in the hexagonal tiling share a common edge, they are called adjacent cells. In one move, Pak Dengklek can move from one cell to an adjacent cell in one of six possible directions (numbered from $1$ to $6$ as shown in the figure below).

Given an action sequence consisting of $N$ actions, Pak Dengklek can construct a territory from the path it produces (which corresponds to a sequence of visited cells in order). The $i$-th action consists of a moving direction $D[i]$ and a move length $L[i]$ in that direction, and the path must satisfy the following properties:
- The path is *closed*, meaning that in the cell sequence, the starting cell and the ending cell (i.e., the initial cell) are the same.
- The path is *simple*, meaning that in the cell sequence, except that the initial cell is visited exactly twice (once as the start and once as the end), every other cell can be visited at most once.
- The path is *exposed*, meaning that in the cell sequence, every cell is adjacent to at least one non-*internal cell* that does not appear in the sequence.
- If a cell does not appear in the cell sequence, and starting from it, without passing through any cell in the sequence, it can reach only finitely many cells (via some number of moves), then we call this cell an *internal cell*.
The figure below shows an example of a path that satisfies the conditions above. In the figure:
- Cell $1$ (pink) is the initial cell.
- The numbered cells (light blue) form the cell sequence, and the numbers indicate the order in which they are visited.
- The cells marked with crosses (dark blue) are *internal cells*.

The constructed territory contains all cells on the path and all internal cells. The distance of a cell $c$ in the territory is defined as the minimum number of moves needed to reach $c$ from the initial cell while only passing through cells contained in the territory. The score of a cell in the territory is defined as $A+d \times B$, where $A$ and $B$ are constants given by Pak Dengklek, and $d$ is the distance of that cell in the territory. The figure below shows the distance of each cell in the territory constructed from the example path above.

Help Pak Dengklek compute the sum of scores of all cells in the territory constructed from the given action sequence. Since the total score may be very large, output the result modulo ${10}^9+7$.
You need to implement the following function:
`int draw_territory(int N, int A, int B, int[] D, int[] L)`
- $N$: the number of actions in the action sequence.
- $A, B$: constants in the score calculation.
- $D$: an array of length $N$, where $D[i]$ is the direction of the $i$-th action.
- $L$: an array of length $N$, where $L[i]$ is the move length of the $i$-th action.
- The function should return the total score of all cells in the territory constructed from the given action sequence, modulo ${10}^9+7$.
- This function will be called exactly once.
Input Format
The sample grader reads input in the following format:
- Line $1$: $N$ $A$ $B$.
- Line $2+i$ ($0 \le i \le N-1$): $D[i]$ $L[i]$.
Output Format
The sample grader prints your answer in the following format:
- Line $1$: the return value of `draw_territory`.
Explanation/Hint
**Sample Explanation**
Consider the following call:
```plain
draw_territory(17, 2, 3,
[1, 2, 3, 4, 5, 4, 3, 2, 1, 6, 2, 3, 4, 5, 6, 6, 1],
[1, 2, 2, 1, 1, 1, 1, 2, 3, 2, 3, 1, 6, 3, 3, 2, 1])
```
This action sequence is the same as the example given in the statement above. The table below lists the score corresponding to each possible distance value in this territory.
| Distance value | Number of cells | Score per cell | Total score |
|:-:|:-:|:-:|:-:|
|$0$|$1$|$2+0 \times 3=2$|$1 \times 2=2$|
|$1$|$4$|$2+1 \times 3=5$|$4 \times 5=20$|
|$2$|$5$|$2+2 \times 3=8$|$5 \times 8=40$|
|$3$|$6$|$2+3 \times 3=11$|$6 \times 11=66$|
|$4$|$4$|$2+4 \times 3=14$|$4 \times 14=56$|
|$5$|$3$|$2+5 \times 3=17$|$3 \times 17=51$|
|$6$|$4$|$2+6 \times 3=20$|$4 \times 20=80$|
|$7$|$4$|$2+7 \times 3=23$|$4 \times 23=92$|
|$8$|$5$|$2+8 \times 3=26$|$5 \times 26=130$|
|$9$|$3$|$2+9 \times 3=29$|$3 \times 29=87$|
|$10$|$4$|$2+10 \times 3=32$|$4 \times 32=128$|
|$11$|$5$|$2+11 \times 3=35$|$5 \times 35=175$|
|$12$|$2$|$2+12 \times 3=38$|$2 \times 38=76$|
The total score is $2+20+40+66+56+51+80+92+130+87+128+175+76=1003$.
Therefore, `draw_territory` should return $1003$.
**Constraints**
- $3 \le N \le 2\times {10}^5$.
- $0 \le A,B \le {10}^9$.
- $1 \le D[i] \le 6$ ($0 \le i \le N-1$).
- $1 \le L[i]$ ($0 \le i \le N-1$).
- The sum of elements in $L$ does not exceed ${10}^9$.
- The path corresponding to the given action sequence is guaranteed to be *closed*, *simple*, and *exposed*.
**Subtasks**
1. (3 points): $N=3$, $B=0$.
2. (6 points): $N=3$.
3. (11 points): The sum of elements in $L$ does not exceed $2000$.
4. (12 points): $B=0$, and the sum of elements in $L$ does not exceed $2\times {10}^5$.
5. (15 points): $B=0$.
6. (19 points): The sum of elements in $L$ does not exceed $2\times {10}^5$.
7. (18 points): $L[i]=L[i+1]$ ($0 \le i \le N-2$).
8. (16 points): No additional constraints.
Translated by ChatGPT 5