Rock Is Push
Contest: Technocup 2020 - Elimination Round 2
Difficulty rating: 2200
Tags: binary search, dp

You are at the top left cell $$$(1, 1)$$$ of an $$$n \times m$$$ labyrinth. Your goal is to get to the bottom right cell $$$(n, m)$$$. You can only move right or down, one cell per step. Moving right from a cell $$$(x, y)$$$ takes you to the cell $$$(x, y + 1)$$$, while moving down takes you to the cell $$$(x + 1, y)$$$.

Some cells of the labyrinth contain rocks. When you move to a cell with rock, the rock is pushed to the next cell in the direction you're moving. If the next cell contains a rock, it gets pushed further, and so on.

The labyrinth is surrounded by impenetrable walls, thus any move that would put you or any rock outside of the labyrinth is illegal.

Count the number of different legal paths you can take from the start to the goal modulo $$$10^9 + 7$$$. Two paths are considered different if there is at least one cell that is visited in one path, but not visited in the other.

Input format:
The first line contains two integers $$$n, m$$$ — dimensions of the labyrinth ($$$1 \leq n, m \leq 2000$$$).

Next $$$n$$$ lines describe the labyrinth. Each of these lines contains $$$m$$$ characters. The $$$j$$$-th character of the $$$i$$$-th of these lines is equal to "R" if the cell $$$(i, j)$$$ contains a rock, or "." if the cell $$$(i, j)$$$ is empty.

It is guaranteed that the starting cell $$$(1, 1)$$$ is empty.

Output format:
Print a single integer — the number of different legal paths from $$$(1, 1)$$$ to $$$(n, m)$$$ modulo $$$10^9 + 7$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first sample case we can't (and don't have to) move, hence the only path consists of a single cell $$$(1, 1)$$$.

In the second sample case the goal is blocked and is unreachable.

Illustrations for the third sample case can be found here: https://assets.codeforces.com/rounds/1225/index.html

Example input:
1 1
.

Example output:
1

Example input:
2 3
...
..R

Example output:
0

Editorial:
Let us compute $$$R_{i, j}$$$ and $$$D_{i, j}$$$ – the number of legal ways to reach the goal assuming:
• we've arrived at the cell $$$(i, j)$$$;
• our next move is right/down respectively;
• our previous move (if there was a previous move) was not in the same direction.
By definition, let us put $$$D_{n, m} = R_{n, m} = 1$$$.
We can see that all rocks reachable from $$$(i, j)$$$ in these assumptions should be in their original places, thus the answer is independent of the way we've reached the cell $$$(i, j)$$$ in the first place.
Recalculation is fairly straightforward. For example, for $$$D_{i, j}$$$ let $$$k$$$ be the number of stones directly below $$$(i, j)$$$. We can make at most $$$n - k - i$$$ moves before we make the turn to the right, thus we have $$$D_{i, j} = \sum_{t = 1}^{n - k - i} R_{i + t, j}$$$. This allows to compute $$$R_{i, j}$$$ and $$$D_{i, j}$$$ with dynamic programming starting from the cells with larger coordinates.
The formula hints at some range summing techniques, like computing prefix sums or maintaing a more sophisticated RSQ structure. However, these are not needed in this problem. Indeed, as we consider summation ranges for $$$D_{i, j}$$$ and $$$D_{i + 1, j}$$$, we can see that they differ by at most one entry on each side. It follows that to compute $$$D_{i, j}$$$, we can take $$$D_{i, j}$$$ and add/subtract at most two values of $$$R_{i + t, j}$$$. The values near the goal cell may need some extra treatment since they are not always a proper range sum. Also, the case $$$n = m = 1$$$ need to be treated separately.
The total complexity is $$$O(nm)$$$ (additional $$$\log$$$ for RSQ should be fine though).