#P11214. Swallowed Diagonals in N-Dimensional Space

    ID: 13282 Type: Default 1000ms 256MiB

Swallowed Diagonals in N-Dimensional Space

Swallowed Diagonals in N-Dimensional Space

Given an nn-dimensional space, where the size of the ii\text{-th} dimension is mim_i. Each position is represented by coordinates $$(x_1, x_2, \dots, x_n)$$ with xi[1,mi]x_i\in[1,m_i]. There is a black hole at position $$(a_1,a_2,\dots,a_n)$$. A position $$(b_1,b_2,\dots,b_n)$$ is on the same diagonal as the black hole if and only if there exists an integer k0k\ge0 such that for every 1in1\le i\le n,

aibi=k.|a_i-b_i|=k.

All positions on the same diagonal (including the black hole itself) will be swallowed. Compute the number of positions swallowed modulo 109+710^9+7.

inputFormat

The input consists of three lines:

  1. The first line contains an integer nn, the number of dimensions.
  2. The second line contains nn integers m1,m2,,mnm_1, m_2, \dots, m_n, representing the size of each dimension.
  3. The third line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n, representing the coordinates of the black hole.

outputFormat

Output a single integer, the number of positions swallowed by the black hole modulo 109+710^9+7.

sample

1
5
3
5