#K60327. Unique Paths in a Grid

    ID: 31062 Type: Default 1000ms 256MiB

Unique Paths in a Grid

Unique Paths in a Grid

Given an (n \times m) grid, you are to determine the number of distinct paths from the top-left corner to the bottom-right corner if you are only allowed to move either right or down. Each cell can be visited at most once. The recurrence relation for the number of paths is given by (dp[i][j] = dp[i-1][j] + dp[i][j-1]), with the boundary conditions (dp[i][0] = dp[0][j] = 1). Since the answer can be very large, report it modulo (10^9 + 7).

For example, in a (2 \times 2) grid, there are exactly 2 unique paths, and in a (3 \times 3) grid there are 6 unique paths.

inputFormat

The input consists of two integers (n) and (m) separated by space, representing the number of rows and columns in the grid respectively. Input is read from standard input (stdin).

outputFormat

Output a single integer: the number of unique paths from the top-left to the bottom-right corner of the grid modulo (10^9+7). The output should be written to standard output (stdout).## sample

2 2
2