All problems

Problem 189

Unique Paths

Medium · 20 pts · Math, Dynamic Programming, Combinatorics

Problem

A robot stands in the top-left cell of a grid with R rows and C columns. In each move it can go one cell right or one cell down. Count the number of different paths that take it to the bottom-right cell, and print the count modulo 1000000007. The number of paths to a cell is the number of paths to the cell above it plus the number to the cell on its left. Every cell in the top row and the left column can be reached in exactly one way. (Mathematically the answer is the binomial coefficient C(R + C - 2, R - 1).) Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

Two integers R and C.

Output

Print the number of paths modulo 1000000007.

Constraints

1 <= R, C <= 1000

Example input

3 7

Example output

28

Explanation

In a 3 x 7 grid the robot makes 2 moves down and 6 moves right, 8 moves in total. Choosing which 2 of the 8 moves go down gives C(8, 2) = 28 paths.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: 0/1 Knapsack Next: Unique Paths with Obstacles