Stuck in an Infinite Grid

5

5 votes
Combinatorics, Easy, Math
Problem

Consider a 2-dimensional Infinite Grid. Find the number of K length walks from the point S(xs,ys) to the point T(xt,yt). Here S and T may be the same point.

You are allowed to move in the four cardinal directions only, i.e. from a point (x,y) you can move to (x+1,y), (x,y+1), (x1,y) and (x,y1). Two points P and Q are adjacent if you can move from one to the other.

Moreover, you can visit the same coordinate multiple times.

A walk of length K can be denoted as a sequence of K+1 coordinates Pi for 0iK, where Pi and Pi+1 are adjacent, for all 0i<K

Two walks of length K, having coordinate sequences as Pi and Qi are considered different if there exists an integer j such that 0jK and PjQj.

Input Format

The first line will contain the integer T, denoting the number of test cases.

Each of the next T lines will contain 5 space-separated integers denoting the value of xs,ys,xt,yt,K respectively.

Output Format

For each test case, print the number of K length walks from S(xs,ys) to T(xt,yt) modulo 109+7

Constraints

1T20

0xs,ys,xt,yt2105

1K2105

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For the first test case, two of the total 9 possible ways are:

(0,0)(0,1)(0,0)(0,1)

(0,0)(0,1)(0,0)(0,1)

Editor Image

?