UCV2013E - Greedy Walking

Reginald is an N-dimensional traveler who wants to return to Filipistonia's Kingdom. He has an Obsessive-compulsive disorder in the way he travels so he can only do it following particular rules:

  1. Every step is exactly one unit long.
  2. He only moves in one dimension at a time.
  3. He only travels along the positive direction for each dimension.

For example, when traveling on a two-dimensional place. He can travel along either the X or the Y axis at any given time, but never on both at the same time. Moreover, since he only travels along the positive direction and every step is one unit long, his only possible moves are (+1, 0) and (0, +1).

Greedy Walking example

As you can see, he is a Greedy Walker: once he makes a decision he assumes it is the correct and he never goes back.


Given a starting position in an N-dimensional space (x1i, x2i ... xni) your task is to count the number of different travels he can make to position (x1f, x2f ... xnf) modulo 1000000007.

Input

The input contains several test cases, each one corresponding to a single travel. Each test case consists of a single line with one integer (1 <= N <= 50) followed by two lines each one with N integers, first line will be initial position and second line will be target position.


You can assume that 0 <= xki <= x1f <= 500 for all k, 1 <= k <= N and Sum(xki-xkf) <= 500.

The end of input is indicated by a test case with N = 0.

Output

For each travel output a single line with one integer, the number of different travels that exist from the initial position to the final position modulo 1000000007.

Example

Input:
2
2 1
5 5
4
0 0 0 0
1 2 3 4
5
1 2 3 4 5
8 5 6 4 8
5
0 0 0 0 0
100 100 100 100 100
0 Output: 35
12600
19219200
257055440

Added by:Hector Navarro
Date:2013-07-22
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Local UCV 2013. Fernando Crema

hide comments
2020-10-03 11:55:33
@infinity_98 it is for n dimensional space
2020-07-14 12:07:42 Shubham Jadhav
Thanks @tezu_singh for the hint :)
2020-07-09 03:28:58
i didn,t understand the input format
5
0 0 0 0 0
100 100 100 100 100
can anyone help me?
2020-06-24 10:43:13
https://www.youtube.com/watch?v=tkhyQXqVxWA&list=PLDDGPdw7e6Ag1EIznZ-m-qXu4XX3A0cIz&index=28
go to this link and see at 12;00 for theory
2015-01-19 17:07:55 Utkarsh Rastogi
my 100th on spoj.. :)
2013-07-29 13:05:39 Hector Navarro
@BroadSword there is only one way to get from a point to itself...
2013-07-29 11:36:40 BroadSword
If initial and target positions are same..the result should be 0 or 1?
2013-07-25 15:44:00 Hector Navarro
@SITH: Your program generates no output... I catched an exception:
java.lang.NumberFormatException

Last edit: 2013-07-25 15:44:18
2013-07-24 06:32:24 SWOOSH!!!
I dont why I am getting WA again and again...@Hector could you explain to me why this is happening? Submission id 9708968
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.