Problems > Routes Across a Grid

Routes Across a Grid

Difficulty: 1400 3000 ms 128 MB

math combinatorics

On an H×W grid you walk from the top-left cell to the bottom-right cell moving only right or down. How many routes exist, modulo 109+7?

Input: one line with two integers H and W.

Output: C(H+W−2, H−1) mod 109+7.

Constraints:

  • 1 ≤ H, W ≤ 106

Sample Tests

Input 1
2 3
Output 1
3
Input 2
1 1
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.