Grid Walker

MEDIUM
G
Time limit: 2.0sMemory limit: 256MB
dpgridcombinatorics

Grid Walker

You start at the top-left cell of an r x c grid and want to reach the bottom-right ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌​‌‌​​​‌‌‌​​​​​‌‌​‌​​​​‌‌​‌‌​​​‌‌‌​​‌​‌‌​​​‌​​​‌‌​​‌‌​​‌‌‌​​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌‌​​​​​‌‌​‌​‌​​‌‌‌​​‌​​‌‌‌​​‌​​‌‌​​‌​cell, moving only right or down one cell at a time. Some cells are blocked (#) and cannot be entered; the rest are open (.). The start and end cells are always open. Count the number of distinct paths, ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌​‌‌​​​‌‌‌​​​​​‌‌​‌​​​​‌‌​‌‌​​​‌‌‌​​‌​‌‌​​​‌​​​‌‌​​‌‌​​‌‌‌​​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌‌​​​​​‌‌​‌​‌​​‌‌‌​​‌​​‌‌‌​​‌​​‌‌​​‌​modulo 1,000,000,007.

Input

Line 1: two integers r c (1 ≤ r, c ≤ 1000) Next r lines: a string of c characters, each . or #.

Output

A single integer: the number of paths modulo 1,000,000,007.

Example

​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌​‌‌​​​‌‌‌​​​​​‌‌​‌​​​​‌‌​‌‌​​​‌‌‌​​‌​‌‌​​​‌​​​‌‌​​‌‌​​‌‌‌​​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌‌​​​​​‌‌​‌​‌​​‌‌‌​​‌​​‌‌‌​​‌​​‌‌​​‌​Input:

3 3
...
.#.
...

Output:

2

Constraints

  • 1 ≤ r, c ≤ 1000 ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌​‌‌​​​‌‌‌​​​​​‌‌​‌​​​​‌‌​‌‌​​​‌‌‌​​‌​‌‌​​​‌​​​‌‌​​‌‌​​‌‌‌​​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌‌​​​​​‌‌​‌​‌​​‌‌‌​​‌​​‌‌‌​​‌​​‌‌​​‌​

Sample Testcases

Case 1

Input
2 2
..
..
Expected Output
2

Case 2

Input
3 3
...
.#.
...
Expected Output
2
Loading Cloud IDE...
Ln 1, Col 1Spaces: 4UTF-8
pythonAuto-Saved

Input

2 2
..
..

Expected Output

2

Run or submit your code to see output here.