A blocked matrix

2.9

22 votes
Strategies and Expected values, Dynamic Programming, Multiplication Rules, Algorithms, Probablity, Math
Problem

You are given a 2D matrix of length n and width m. There is exactly one blocked cell in a 2D matrix that is uniformly chosen among all points except (1,1) and (n,m).

Find the expected number of paths from (1,1) to (n,m). You can only move rightwards and downwards. If the expected number of paths is pq in the simplest form, then print (p.q1) modulo 1000000007 where q1 is the multiplicative inverse of q modulo 1000000007.

Input format

  • Each test contains multiple test cases. The first line contains T denoting the number of test cases. (1T100000)
  • The first and only line of each test case contains two space-separated integers n(2n100000) and m(2m100000) representing the length and the width of the 2D matrix respectively.

Output format

For each test case, print a single line containing one integer (p.q1) modulo 1000000007 where pq is the expected number of paths.

Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

When cell (1,2) is blocked there is exactly one path from (1,1) to (2,2).

When cell (2,1) is blocked there is exactly one path from (1,1) to (2,2).

Therefore, expected number of path is 1.

Editor Image

?