problems / 138

Paths Through a Blocked Grid

easy dp combinatorics arrays 1000 ms · 256 MB

Paths Through a Blocked Grid

You are given a grid of n rows and m columns. Every cell is either free (.) or blocked (#).

A path starts in the top-left cell, ends in the bottom-right cell, and at every step moves exactly one cell right or down. A path is allowed only if every cell it visits, including the first and the last, is free.

Count the allowed paths. The number can be huge, so print it modulo 10^9+7.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

Each of the next n lines contains a string of exactly m characters, each . or #, describing one row of the grid from top to bottom.

Output

Print one integer: the number of allowed paths modulo 10^9+7. If there are none, which includes the case where the top-left or the bottom-right cell is blocked, print 0.

Notes

In the first example the grid has 10 right/down paths in total; 6 of them pass through the blocked cell, leaving 4.

In the second example the three paths are RRRDDD, RDDRRD and RDDRDR, where R is a step right and D a step down.

In the third example the bottom-right cell is blocked, so no path is allowed.

When n = m = 1 there is exactly one path (the empty one) if the only cell is free.

Example 1

Input

3 4
....
.#..
....

Output

4

Example 2

Input

4 4
....
#.#.
....
.#..

Output

3

Example 3

Input

2 3
...
..#

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

The number of paths is far too large to enumerate, even on an open grid. Think about the last step into any free cell: from which cells can you arrive there, and how do those two groups of paths relate?

Hint 2

Let each free cell hold the number of valid paths from the start to it, computed from the cell above and the cell to its left. Fill the grid in row order, reduce modulo 10^9+7, and note that one row of storage is enough.

report an issue Prefer your own editor? sooks prep 138 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.