We need to understand the problem. There's a ring at origin (0,0). They take turns moving the ring. Alfi (odd steps) and Roy (even steps). They move in certain directions and with displacements determined as described.
We need the perpendicular distance of the ring from the Y-axis after step N. That means the x-coordinate's absolute value? Usually perpendicular distance from Y-axis is the absolute x-coordinate (distance from the line x=0). So it's |x|. But sample outputs: after steps 1,2,3,4 they all output 1. Let's check if that's absolute x or maybe x coordinate? Could be absolute distance, but if after step 1, Alfi moves right by 1, ring at (1,0) -> distance from Y-axis = 1. After step 2, Roy moves up by 1, so from (1,0) to (1,1) -> x-coordinate is still 1, distance = 1. After step 3, Alfi moves left? But need to check direction alternation. Alfi moves left or right alternating, starting with right on her first step. So step1: right. Then step3: since last step she moved right, now she must move left. Displacement: For Alfi's turn other than first step, she sums up her last displacement and Roy's last displacement, and moves the ring by that value. So on step3, Alfi's last displacement was at step1: 1 (right positive direction? but magnitude). Roy's last displacement: at step2, Roy moved up, displacement magnitude 1. Sum = 1+1 = 2. She moves left, so displacement vector: (-2, 0). Starting from (1,1), after step3: (1-2, 1) = (-1, 1). So distance from Y-axis = |x| = 1. After step4, Roy's turn. He moves up or down alternating, initially up on step2. So step2: up. So step4: he moves down (alternate). Displacement: For Roy's turn other than first step, he sums his last displacement and Alfi's last displacement. His last displacement: at step2, magnitude 1. Alfi's last displacement: at step3, magnitude 2 (but direction left, but magnitude used). So sum = 1+2 = 3. He moves down: vector (0, -3). Starting from (-1,1) after step4: (-1, 1-3) = (-1, -2). Distance from Y-axis = | -1 | = 1. So output is 1. So indeed it's absolute x-coordinate.
Thus the answer for each N is the absolute value of the x-coordinate after N steps. Since the x-coordinate may be negative or positive, but distance is positive, so we output absolute value modulo mod? But careful: if we output modulo, absolute value modulo mod is just the positive value mod mod. Since x can be negative, absolute value is positive. But we need to compute it modulo 1e9+7. Since numbers may be huge, but we can compute x coordinate maybe using recurrence.
But note: The distance is absolute x-coordinate. But the problem might ask for perpendicular distance, which is always non-negative. In sample they output 1 for all four steps. So indeed it's absolute.
Now we need to compute for large N up to 1e9. Q up to 1e4. So we need a formula or fast method.
We need to model the process. Let's denote step numbers: 1,2,3,4,... Alfi (odd), Roy (even). We have movements:
Define A_n as displacement vector at step n: for odd n, Alfi moves either right (+dx,0) or left (-dx,0). For even n, Roy moves either up (0,+dy) or down (0,-dy). The magnitude (dx,dy) are determined.
Let’s denote the magnitude of the displacement (the absolute distance moved) at each step, irrespective of direction, as d_n (positive integer). Directions: For Alfi, step1: right (+). For step3: left (-). Step5: right? Since alternates: right, left, right, left... So direction sign for Alfi: sign_A(n) = +1 if (n-1)/2 is even? Let's derive. Alfi's steps: odd numbers: 1,3,5,7,... She starts right at step1, then left at step3, then right at step5, etc. So sign alternates: +, -, +, -, ... So sign_A(n) = (+1) if (n+1)/2 is odd? Actually step1: (1+1)/2=1 odd -> +. Step3: (3+1)/2=2 even -> -. Step5: (5+1)/2=3 odd -> +. So sign = (-1)^{((n+1)/2)+1}? Let's not get lost, we can handle direction later.
For Roy, even steps: 2,4,6,... Starting with up at step2, then down at step4, then up at step6,... So sign_R(n): up is positive y, down negative y. So step2: +, step4: -, step6: +,... So sign alternates: +, -, +, -. So sign_R(n) = +1 if n/2 is odd? Step2: n/2=1 odd -> +. Step4: n/2=2 even -> -. Step6: n/2=3 odd -> +. So sign_R(n) = (-1)^{(n/2)+1} maybe.
Now the magnitude d_n is defined as:
-
For step1: d_1 = 1
-
For step2: d_2 = 1
-
For odd n>1: d_n = d_{n-2} + d_{n-1}? Let's see: Alfi's step (odd) sums her last displacement (which is d_{n-2} because her previous step was n-2, since she moves on odd steps) and Roy's last displacement (d_{n-1}, the immediate previous step by Roy, which is even). So d_n = d_{n-2} + d_{n-1}.
-
For even n>2: d_n = d_{n-2} + d_{n-1}. Because Roy sums his last displacement (d_{n-2}, his previous even step) and Alfi's last displacement (d_{n-1}, the immediate previous odd step). So it's symmetric.
Thus for all n >= 3, d_n = d_{n-1} + d_{n-2}. With d_1 = 1, d_2 = 1.
So d_n = F_n (the Fibonacci numbers) if we take F_1=1, F_2=1, F_3=2, etc. So d_n = Fibonacci number? Indeed d_1=1, d_2=1, d_3=2, d_4=3, d_5=5, d_6=8, ... So d_n = F_n (with F_1=1, F_2=1). Good.
Now the direction: The x-coordinate after step N is sum of contributions from Alfi steps (odd steps) with appropriate signs. Similarly y-coordinate from Roy steps. We need |x_N|.
Let’s denote x_n after step n. Initially x_0=0. For each odd step i, we add sign_A(i) * d_i. For each even step, x doesn't change (since Roy moves vertical). So x_N = sum over odd i <= N of sign_A(i) * d_i.
Similarly y_N = sum over even i <= N of sign_R(i) * d_i.
We need |x_N| mod M where M = 1e9+7. Since N up to 1e9, we need closed form for sum of signed Fibonacci numbers over odd positions with alternating signs.
Define indices:
Let’s list odd steps and signs:
Step1: sign +, d=1 => +1
Step3: sign -, d=2 => -2
Step5: sign +, d=5 => +5
Step7: sign -, d=13? Actually d_7 = 13 (since Fib: 1,1,2,3,5,8,13,...). Step7: sign =? pattern: +, -, +, -, ... So step7 sign = - (since step5 +, step7 -). So -13
Step9: sign +, d_9 = 34 => +34
...
So x after odd steps: after step1: 1; after step3: 1 - 2 = -1; after step5: -1 + 5 = 4; after step7: 4 -13 = -9; after step9: -9+34=25; after step11: 25-89 = -64; after step13: -64+233=169; after step15: 169-610 = -441; ... So pattern: x after odd steps seems to be: 1, -1, 4, -9, 25, -64, 169, -441,... That looks like squares: 1^2, (-1)^2? Actually 1=1^2, -1? absolute 1? but 1^2=1, but we have -1. Then 4=2^2, -9 = -(3^2), 25=5^2, -64=-(8^2), 169=13^2, -441=-(21^2). So absolute values are squares of Fibonacci numbers? Let's check: Fibonacci numbers: 1,1,2,3,5,8,13,21,34,... Indeed squares: 1^2=1, 2^2=4, 3^2=9, 5^2=25, 8^2=64, 13^2=169, 21^2=441. And signs alternate? After step1 (odd index 1) positive, step3 negative, step5 positive, step7 negative,... So absolute x after odd step n (odd) = F_k^2? Let's index: For odd step number n = 2k-1? Since odd numbers: 1,3,5,7,... correspond to k=1,2,3,4,... Then observed: step1 (k=1): |x| = 1 = F_2? Actually F_2=1, but 1 also F_1=1. Hmm we have Fib numbers: F_1=1, F_2=1, F_3=2, F_4=3, F_5=5, F_6=8, F_7=13, F_8=21, F_9=34,... For step1: |x| = 1 (maybe F_1^2 or F_2^2). For step3: |x| = 1? Actually after step3, |x| = |-1| =1. That's 1 again. That's not square of 2. Wait we need to check: After step3, x = -1, absolute = 1. That is 1^2 =1. But which Fibonacci? F_2=1 maybe. After step5: x=4, absolute=4 = 2^2, and F_3=2. After step7: absolute=9 = 3^2, F_4=3. After step9: absolute=25 =5^2, F_5=5. After step11: absolute=64=8^2, F_6=8. After step13: absolute=169=13^2, F_7=13. So pattern: For odd step N = 2k+1? Let's list:
k index from 0? step1: k=0? We see:
If we let n = 2m-1 (odd steps). Then m=1 => n=1: |x| = 1 = (F_? )? For m=1, maybe |x| = F_1? F_1=1. m=2 => n=3: |x| = 1 = F_2? F_2=1. m=3 => n=5: |x| = 2^2 = 4? But F_3=2. So not consistent.
Maybe |x| after step n (odd) = F_{ (n+1)/2 }^2? For n=1, (1+1)/2=1 => F_1^2=1 ok. n=3 => (3+1)/2=2 => F_2^2=1 ok. n=5 => (5+1)/2=3 => F_3^2=4 ok. n=7 => (7+1)/2=4 => F_4^2=9 ok. n=9 => 5 => F_5^2=25 ok. So yes! So after odd step N, |x_N| = (F_{(N+1)/2})^2.
Now what about after even steps? After step2 (even), x=1 (from step1), so |x|=1. That is not a square of Fibonacci? F_? F_1=1, (1+? maybe after even step N, |x| = F_{N/2} * F_{N/2+1}? Let's check: step2: N=2 => N/2=1 => maybe product F_1 * F_2 = 11=1, works. Step4: after step4, we had x=-1, absolute=1. N/2=2 => F_2 * F_3 = 12=2, but absolute is 1, not 2. So not that. Let's compute more: After step6: we have step5 gave x=4, step6 Roy moves up/down? Actually step6 is Roy's turn: even, moves direction? Step6: Roy alternates: step2 up, step4 down, step6 up. Displacement d_6=8 (since Fib). So after step6, we add (0,+8) to previous coordinates after step5: after step5, we had (4,? ) Let's compute step by step to confirm.
We'll compute full coordinates for first few steps.
Define steps:
Step1: Alfi, right, d1=1 => (1,0)
Step2: Roy, up, d2=1 => (1,1)
Step3: Alfi, left, d3=2 => (-1,1)
Step4: Roy, down, d4=3 => (-1,-2)
Step5: Alfi, right, d5=5 => (4,-2)
Step6: Roy, up, d6=8 => (4,6)
Step7: Alfi, left, d7=13 => (-9,6)
Step8: Roy, down, d8=21 => (-9,-15)
Step9: Alfi, right, d9=34 => (25,-15)
Step10: Roy, up, d10=55 => (25,40)
Step11: Alfi, left, d11=89 => (-64,40)
Step12: Roy, down, d12=144 => (-64,-104)
Step13: Alfi, right, d13=233 => (169,-104)
Step14: Roy, up, d14=377 => (169,273)
Step15: Alfi, left, d15=610 => (-441,273)
... etc.
Now check x after even steps:
Step2: x=1
Step4: x=-1 => |x|=1
Step6: x=4 => |x|=4
Step8: x=-9 => |x|=9
Step10: x=25 => |x|=25
Step12: x=-64 => |x|=64
Step14: x=169 => |x|=169
So pattern: |x| after even steps: step2:1, step4:1, step6:4, step8:9, step10:25, step12:64, step14:169. That matches squares: 1^2,1^2,2^2,3^2,5^2,8^2,13^2. Which Fibonacci numbers? For step2: (N/2)=1 => F_1^2=1; step4: N/2=2 => F_2^2=1; step6: N/2=3 => F_3^2=4; step8: N/2=4 => F_4^2=9; step10: N/2=5 => F_5^2=25; step12: N/2=6 => F_6^2=64; step14: N/2=7 => F_7^2=169. So after even step N, |x_N| = (F_{N/2})^2 as well? Let's verify: N=2 => N/2=1 => F_1^2=1, correct. N=4 => F_2^2=1, correct. N=6 => F_3^2=4, correct. So yes! After even steps, |x| = (F_{N/2})^2.
But careful: For N=2, we have x=1, F_1=1. For N=4, x=-1, absolute=1 = F_2^2=1. So pattern holds.
Now, what about odd steps we earlier had |x| = (F_{(N+1)/2})^2? For N=1, (1+1)/2=1 => F_1^2=1; N=3 => (3+1)/2=2 => F_2^2=1; N=5 => 3 => F_3^2=4; N=7 => 4 => F_4^2=9; N=9 =>5 => F_5^2=25; So it matches.
Thus, after any step N, the absolute x-coordinate (distance from Y-axis) equals (F_{ceil(N/2)})^2? Let's check: For N=1, ceil(1/2)=1 => F_1^2=1; N=2, ceil(2/2)=1 => F_1^2=1; N=3, ceil(3/2)=2 => F_2^2=1; N=4, ceil(4/2)=2 => F_2^2=1; N=5, ceil(5/2)=3 => F_3^2=4; N=6, ceil(6/2)=3 => F_3^2=4; N=7, ceil(7/2)=4 => F_4^2=9; N=8, ceil(8/2)=4 => F_4^2=9; N=9, ceil(9/2)=5 => F_5^2=25; So pattern: |x_N| = (F_{⌈N/2⌉})^2.
But wait: For N=3, we computed x = -1, absolute=1, matches. So indeed.
Thus answer for each query N is (F_{⌈N/2⌉})^2 mod M.
But we must verify further: For N=0? Not in constraints.
Now we need to compute Fibonacci numbers up to index ceil(N/2) which can be up to 5e8 (since N up to 1e9, ceil(N/2) up to 5e8). That's still large; need fast exponentiation using matrix exponentiation modulo M for each query? Q up to 1e4, and N up to 1e9, so we need to answer quickly. Could precompute using fast doubling method for Fibonacci numbers modulo M, which is O(log n) per query, which is fine: 1e4 * log(5e8) ~ 1e4 * 30 = 3e5 operations, trivial.
But we need to compute square mod M.
Thus for each query N, compute k = (N+1)//2? Actually ceil(N/2) = (N+1)//2 in integer division (floor). For N odd: (N+1)/2 integer; for N even: N/2, but (N+1)//2 works for even? Example N=2: (2+1)//2 = 3//2 =1, which is ceil(2/2)=1, correct. N=4: (4+1)//2=5//2=2, correct. So yes, k = (N+1)//2.
Then compute F_k modulo M, then compute (F_k)^2 % M.
But careful: The problem states "perpendicular distance of Ring from Y-axis after that step." That is the distance, which is absolute value. Since we are taking modulo 1000000007, and the distance is always positive, we can just compute the square modulo mod. However, is it exactly (F_k)^2, or could it be something else for larger N? Let's try to derive more formally.
We have d_n = F_n with F_1=1, F_2=1.
Now define x_n = sum_{odd i ≤ n} s_i * d_i, where s_i = ±1 alternating starting with +1 at i=1.
Define index m for odd steps: let i = 2j-1, where j >=1. Then d_{2j-1} = F_{2j-1}. And s_{2j-1} = (-1)^{j-1} maybe? Because j=1 -> +, j=2 -> -, j=3 -> +, so s_{2j-1} = (-1)^{j+1}? Actually + for j=1: (-1)^{0}=1 if exponent j-1. So s = (-1)^{j-1}.
Thus x after step N (if N odd, last odd step index = 2M-1 where M = (N+1)/2; if N even, last odd step index = N-1? Actually after even step, last odd step is N-1, but x doesn't change on even step, so x_N = x_{N-1} if N even. So x_N = sum_{j=1}^{⌈N/2⌉} (-1)^{j-1} F_{2j-1}.
Because number of odd steps up to N is ⌈N/2⌉. For N odd, it's exactly (N+1)/2; for N even, it's N/2 (since odd steps are 1,3,...,N-1, count = N/2). So indeed let M = ceil(N/2) = number of odd steps taken so far. Then x_N = sum_{j=1}^{M} (-1)^{j-1} F_{2j-1}.
We need to evaluate S_M = ∑{j=1}^{M} (-1)^{j-1} F{2j-1}. Is this equal to (F_M)^2? Let's check known identities. There is identity: F_1 - F_3 + F_5 - ... + (-1)^{n-1} F_{2n-1} = (-1)^{n-1} F_n^2? Actually I recall identities: Sum_{i=1}^{n} F_{2i-1} = F_{2n} (for non-alternating). Alternating sum: ∑{k=1}^{n} (-1)^{k-1} F{2k-1} = F_n^2? Let's test: n=1: F_1^2=1; sum = F_1=1 => yes. n=2: F_2^2=1; sum = F_1 - F_3 = 1 - 2 = -1? That's -1, not 1. But absolute value 1. Wait S_2 = 1 - 2 = -1, but |S_2| =1 = F_2^2. So maybe S_n = (-1)^{n-1} F_n^2? Check: n=2: (-1)^{1} F_2^2 = -1 => matches S_2 = -1. n=1: (-1)^{0} F_1^2 = 1 => matches S_1=1. n=3: S_3 = F_1 - F_3 + F_5 = 1 - 2 + 5 = 4. (-1)^{2} F_3^2 = + F_3^2 = 4 => matches. n=4: S_4 = 1 - 2 + 5 - 13 = -9. (-1)^{3} F_4^2 = -9? F_4=3, square=9, times -1 = -9 => matches. So indeed S_M = (-1)^{M-1} (F_M)^2.
Therefore x_N = (-1)^{M-1} (F_M)^2. So absolute x_N = (F_M)^2. And M = ceil(N/2). So the answer is (F_{(N+1)//2})^2 mod MOD.
Thus solved.
Now we need to compute Fibonacci numbers modulo MOD, for index up to 5e8. Use fast doubling method.
Let's implement Fibonacci fast doubling:
We need F_n mod MOD. Use function fib(n) returning pair (F_n, F_{n+1}) modulo MOD. Recurrence:
If n == 0: return (0, 1)
Else:
(a, b) = fib(n // 2) // a = F_k, b = F_{k+1} where k = floor(n/2)
c = a * ((b * 2 - a) % MOD) maybe careful.
Standard fast doubling formulas:
For even: n = 2k:
F_{2k} = F_k * (2F_{k+1} - F_k)
F_{2k+1} = F_{k+1}^2 + F_k^2
For odd: n = 2k+1:
F_{2k+1} = F_{k+1}^2 + F_k^2
F_{2k+2} = F_{k+1} * (2F_k + F_{k+1})? Actually we can compute from same pair.
But simpler: compute (F_n, F_{n+1}) with:
def fib(n):
if n == 0:
return (0, 1)
else:
a, b = fib(n >> 1)
c = a * ( (2b - a) % MOD ) % MOD
d = (aa + b*b) % MOD
if n % 2 == 0:
return (c, d)
else:
return (d, (c + d) % MOD)
Yes.
But careful with mod and negative: (2*b - a) may be negative; we take modulo with positive.
Now we need only F_M. So we can call fib(M) and get the first element.
Now M can be up to 500 million, recursion depth log2(5e8) ~ 29, fine.
Now answer = (F_M * F_M) % MOD.
Now let's verify sample: Q=4, N=1,2,3,4. For N=1: M=(1+1)//2=1, F_1=1, square=1. N=2: M=1, same. N=3: M=2, F_2=1, square=1. N=4: M=2, square=1. Matches.
But before finalizing, we need to consider if N can be very large like 1e9, M up to 5e8, but fib modulo 1e9+7 is fine. However, might there be any overflow? Python's big integers fine.
But we need to check if there is any nuance about direction: Alfi moves left or right alternatively, starting right. That gives sign pattern we used. Yes.
Now, we should also confirm for some larger N using our computed numbers. Let's test N=5: M=3, F_3=2, square=4. According to our earlier computation, after step5 x=4, absolute=4. So answer 4.
N=6: M=3, square=4, after step6 x=4, absolute=4. So answer 4.
N=7: M=4, F_4=3, square=9, after step7 x=-9, abs=9.
So looks correct.
Now, is there any possibility that after some step, the x coordinate could be something else? We derived identity S_M = (-1)^{M-1} F_M^2. This is a known identity. So good.
Thus solution: For each N, compute k = (N+1)//2, compute F_k mod 1e9+7, output (F_k * F_k) % MOD.
Complexities: O(Q log N) time, O(log N) recursion depth.
But can we precompute something? Not needed.
But wait, constraints: Q up to 10000, N up to 1e9. That's fine.
Now we need to be careful with the definition of Fibonacci numbers: F_1 = 1, F_2 = 1. In fast doubling, we need to handle base case. Typically fast doubling with n=0 gives (0,1). Then F_1 = 1, F_2 = 1. Good.
Check: using fib(1): returns? Let's test: n=1, recursion: n>>1 = 0, returns (0,1). Then a=0, b=1. c = a*(2b - a) = 0(2-0)=0. d = a^2 + b^2 = 0+1=1. Since n%2==1, return (d, c+d) = (1, 0+1=1). So F_1=1, F_2=1. Good.
For n=2: n>>1=1 => returns (1,1). a=1, b=1. c = 1*(21 - 1)=1(2-1)=1. d = 1+1=2. n even => return (c,d) = (1,2). So F_2=1, F_3=2. Good.
So works.
Thus answer.
Now potential pitfalls: The output is distance, which is absolute x. However, if we compute (F_k)^2 mod MOD, that's positive. So fine.
But we should confirm that there is no offset: After step 0 (before any steps), distance is 0, but not needed.
Edge case: N=0 not present.
Now is there any possibility that the distance is defined as distance from Y-axis, which is sqrt(x^2 + y^2?) No, perpendicular distance from Y-axis means shortest distance to that axis, which is |x|. So it's x-coordinate absolute.
Thus solved.
But before final answer, let's think if there could be any twist: The problem statement: "Alfi places a ring at origin of a Cartesian Plane. Both Alfi and Roy move the ring alternatively. Alfi takes odd steps and Roy takes even steps." Then direction and displacement explained. "Given the step number N, what is the perpendicular distance of Ring from Y-axis after that step." So as we derived.
Now check sample: They gave output 1 for 1,2,3,4. So matches.
Now, for larger N, we could compute by recurrence and see pattern. It's indeed squares of Fibonacci numbers.
Thus solution.
Now consider mod 1000000007. Since we are squaring, note that (F_k mod MOD)^2 mod MOD is correct. No overflow issues.
Now we need to implement efficiently.
We can implement a function fib(n) using iterative approach with fast doubling non-recursively to avoid recursion depth? Recursion depth is fine.
But Python recursion depth default is 1000, but we have depth only about 30, so fine. However, using recursion may be slower due to function calls, but 1e4 * 30 = 300k calls, it's fine.
Alternatively, implement iterative fast doubling using loops. But recursion is simpler.
We should also consider memoization? Since queries may have same k? Q up to 1e4, could have repeated N. So we could cache results for different k. But not necessary; but could use dictionary to store computed fib(k) to avoid recomputation. Since k up to 5e8, not many repeats likely but possible. We'll implement caching to be safe.
But caching across multiple test cases: use a dict to store computed Fibonacci numbers for indices. Since fib(n) returns pair, we can store for each n the (F_n, F_{n+1}) to speed up repeated calls. However, note that the fast doubling recursion naturally caches if we memoize each n? But recursion will compute for many n (like binary tree). If we store results for each n encountered, subsequent fib calls for same or different numbers might reuse. But careful: For different n, the recursion paths share subproblems? For example, if we call fib(5) and then fib(6), they share some n. So memoization could help. However, given Q=10000 and each call is O(log n), total operations ~ 300k, not heavy. So we can skip memoization. But we can implement simple memoization to be safe.
I'll implement a dictionary memo = {} that stores (F_n, F_{n+1}) for n. In fib(n), if n in memo, return memo[n]. Else compute recursively and store.
But note that Python recursion will call on n//2, and we may have already stored that from previous queries, so subsequent queries may be faster. Not needed but nice.
Now consider memory: memo may hold up to O(log max_n) * Q? Actually each query may add many new keys but eventually all queries up to 5e8 have overlapping binary representations; the total distinct n visited across many queries could be up to about 2 * number_of_bits * Q? But if we cache globally, the total distinct n visited across all queries might be limited because all numbers share the same binary prefixes? For random numbers up to 5e8, the set of numbers visited by fast doubling recursion includes all numbers along the path from n down to 0. For many n, paths may overlap heavily. However, worst-case, we could have many distinct numbers. But 5e8 is large, but the number of distinct numbers that can appear as floor division by 2 repeatedly from numbers up to 5e8 is bounded by about 2 * bits * number of queries? Actually each n's binary representation leads to numbers that are prefixes of its binary. For two different numbers, their recursion tree nodes are numbers formed by taking the number and repeatedly shifting right (i.e., floor division by 2). This yields all numbers that are prefixes of the binary representation. For random numbers, there can be many distinct prefixes, but maximum number of distinct numbers from 0 to 5e8 is obviously at most 5e8+1, but we won't visit all; we only visit numbers that appear as n//2^k for some n. That's at most the set of all numbers that are floor divisions of any n in the query set. For Q=1e4, each n gives about 30 numbers, so at most 300k distinct numbers. So caching is fine.
Thus implement memo.
Now note: we need only F_k. But we could also compute directly using matrix exponentiation. But fast doubling is simpler.
Now we also need to consider if there is any twist regarding negative numbers modulo? Not needed.
Thus final answer: For each N, compute k = (N+1)//2, compute f = fib(k)[0] (or first element), ans = (f*f) % MOD, print.
Now let's test with some values:
N=1 -> k=1 -> f=1 -> ans=1
N=2 -> k=1 -> 1
N=3 -> k=2 -> f=1 -> 1
N=4 -> k=2 ->1
N=5 -> k=3 -> f=2 ->4
N=6 -> k=3 ->4
N=7 -> k=4 -> f=3 ->9
N=8 -> k=4 ->9
N=9 -> k=5 -> f=5 ->25
N=10 -> k=5 ->25
Matches.
Now we should also test large N. For N=1e9, k=500000000, compute fib(500000000) mod 1e9+7. Should be fine.
Now consider possible integer overflow in intermediate multiplication: a*a may be large, but Python big ints handle it. However, we take modulo after each multiplication.
Now edge: MOD = 1000000007.
Thus code.
But wait: There's a nuance: The problem states "perpendicular distance of Ring from Y-axis". Usually distance is non-negative, but could it be the distance as a positive number, not absolute x? Our computed value is absolute x. But is it possible that the ring moves such that x coordinate becomes negative? Yes, but distance is positive. So we output absolute value. However, we are computing (F_k)^2 which is always positive. So fine.
Now let's think about the y coordinate? Not needed.
Now we also must confirm that the recurrence for displacements is indeed d_n = d_{n-1} + d_{n-2} with d_1=1,d_2=1. Yes.
Now we need to ensure that we interpreted direction correctly: Alfi alternates left/right, starting right. That gave signs we used. If it were starting left, sign would be opposite but absolute same. So fine.
Now final answer.
But wait: Could there be any scenario where the distance from Y-axis is not simply |x|? Because the Y-axis is the vertical line x=0. The shortest distance from a point (x,y) to that line is indeed |x|. So yes.
Now we need to output modulo 1e9+7. Since our distance is integer (square of Fibonacci), mod is fine.
Now the problem statement might be ambiguous: "perpendicular distance of Ring from Y-axis" could be interpreted as the distance measured perpendicularly, which is |x|. So yes.
Now we can produce solution.
One more check: The sample explanation says "From the figure its clear that perpendicular distance of Ring from Y-axis after steps 1, 2, 3 and 4 are all 1." So matches.
Now implement.
Implementation steps:
- Read Q
- For each query, read N.
- Compute k = (N + 1) // 2
- Compute f = fib(k) using fast doubling with memoization.
- ans = (f * f) % MOD
- Print ans
We must define fib(n) that returns F_n.
We'll use dictionary memo: memo = {0: (0, 1)}? Actually base case n=0 returns (0,1). But we might store computed results.
Define:
def fib(n):
if n in memo:
return memo[n]
if n == 0:
return (0, 1)
else:
a, b = fib(n >> 1)
# compute c = F_{2k}, d = F_{2k+1} where k = n>>1
# Use formulas:
# F_{2k} = F_k * (2*F_{k+1} - F_k)
# F_{2k+1} = F_k^2 + F_{k+1}^2
# But careful: we have a = F_k, b = F_{k+1}
# So:
c = a * ((2 * b - a) % MOD) % MOD
d = (a * a + b * b) % MOD
if n & 1 == 0:
memo[n] = (c, d)
return (c, d)
else:
# For odd n = 2k+1: F_{2k+1}=d, F_{2k+2}=c+d
memo[n] = (d, (c + d) % MOD)
return (d, (c + d) % MOD)
But careful: In the odd case, we need to return (F_{2k+1}, F_{2k+2}) which is (d, c+d). So yes.
But we must ensure that the formulas for c and d are correct modulo. Also (2b - a) might be negative; we do (2b - a) % MOD, but better: (2b - a + MOD) % MOD? Actually we can compute (2b - a) % MOD directly in Python: ((2b - a) % MOD) works fine because % yields non-negative. But careful: if we compute (2b - a) before modulo, it's an integer, possibly negative, but then we take % MOD which gives non-negative. However, in expression a * ((2b - a) % MOD) % MOD, we first take mod, then multiply. That's fine. But we could also compute c = (a * ( (2b - a) % MOD )) % MOD.
Better: t = (2 * b - a) % MOD; c = a * t % MOD; d = (a * a + b * b) % MOD.
But note: The formula for F_{2k} is: F_{2k} = F_k * (2 * F_{k+1} - F_k). That's standard. So we do that.
Now confirm with small n: n=1: a,b from n>>1=0 => (0,1). t = (21 - 0)=2 % MOD=2; c = 02=0; d = 0+1=1. Since n odd, return (d, c+d) = (1, 0+1=1) -> correct.
n=2: call fib(1) -> returns (1,1) (since memo might store). Actually compute recursively: n=2: a,b from fib(1) => (1,1). t = (21 -1)=1; c=11=1; d=1+1=2. n even, return (1,2) -> correct.
n=3: a,b from fib(1) -> (1,1) (since 3>>1=1). t=1; c=1; d=2. n odd => return (d, c+d) = (2,3) -> F_3=2, F_4=3 correct.
n=4: a,b from fib(2) -> (1,2). t = (22 -1)=3; c=13=3; d=1+4=5. n even => (3,5) -> F_4=3, F_5=5 correct.
So good.
Now caching: We can store memo for each n computed. But careful: In recursion, we may call fib(n>>1) which may be already computed from other calls. And after we compute, we store for n.
One more thing: Since we are doing many queries, we may want to compute k for each query and then call fib(k). The memo dictionary will persist across queries, so subsequent queries with same k will be O(1). Good.
Now ensure that we use iterative reading and writing for speed.
Now final code.
But also consider if N is extremely large, (N+1)//2 may exceed typical integer? Python fine.
Now any other nuance? The problem statement: "Initially both of them displace the ring by 1 unit." That sets d1=1, d2=1. Good.
Now sample output matches.
Thus finalize.
But before concluding, maybe there is a catch: The perpendicular distance could be the distance from the Y-axis, but if the ring is at (x,y), that distance is sqrt(x^2 + (y-0? )? Actually distance from a point to a line (Y-axis) is absolute x. No sqrt needed. So it's just |x|. So integer.
Thus answer is integer.
But then why would they require modulo? Because (F_k)^2 can be huge for large k (k up to 5e8, Fibonacci grows exponentially, so number of digits ~ 0.208k, huge). So we compute mod.
Now, maybe there is alternative interpretation: distance could be Euclidean distance from the Y-axis? That's still |x|. So ok.
Thus solution is as above.
Now implement in code.
But wait, maybe they want the perpendicular distance from the ring to the Y-axis, which is the length of the perpendicular segment from the point to the Y-axis. That is |x|. So yes.
Now, could there be a scenario where the ring moves in a direction not aligned with axes? No, Alfi moves horizontally, Roy vertically. So only axis-aligned moves.
Now check again the initial direction: "Alfi always moves either left or right alternatively, i.e. if she moved left in her last step, she will move right in her current step. Initially she moves right. Roy always moves up or down alternatively, i.e if he moved up in his last step, he will move down in his current step. Initially he moves up." So our sign pattern is correct.
Now we should consider the possibility that on later Alfi steps, she sums up her last displacement and Roy's last displacement. That is the magnitude. So d_n = d_{n-2} + d_{n-1}. So it's Fibonacci.
Now we also need to confirm that the signs of Alfi alternate regardless of the displacement magnitude. Yes, direction alternates left/right, not dependent on magnitude.
Now, one more check: For Roy, direction alternates up/down. So y-coordinate: sum of signed d_i on even steps with alternating signs. That's a similar alternating sum of F_{2j}? Actually even indexed Fibonacci numbers? d_{2j} = F_{2j}. And signs: step2 +, step4 -, step6 +,... so sign = (-1)^{j-1} maybe? Since j=1 -> +, j=2 -> -, j=3 -> +. So y after N steps: if N even, last even step index = N; if N odd, last even step = N-1. So y_N = sum_{j=1}^{⌊N/2⌋} (-1)^{j-1} F_{2j}. That's a known identity: sum_{j=1}^{n} (-1)^{j-1} F_{2j} = (-1)^{n-1} F_n * F_{n+1}? Something like that. But not needed.
Now final solution.
But there's something: The problem says "print the result of each query in a new line." So we output ans per query.
Now we should consider using fast I/O: sys.stdin.read() and splitting.
Now maybe the answer is simply Fibonacci squared. However, I should test with some bigger values manually:
Compute F_6=8, square=64. For N=11, M=6? Actually N=11 => M=(11+1)//2=6 => F_6=8, ans=64. According to earlier step11 x=-64, abs=64. Good.
So code.
Now I will produce final answer.
But one more nuance: The modulo is 1e9+7, and we are squaring modulo. However, if distance is negative? No, distance is positive.
Now, just to be thorough, consider if after some steps the x-coordinate could be zero? Could it be zero? From pattern, absolute x is square of Fibonacci, which is 1,1,4,9,... never zero after step >0. So not zero.
Now implement.
But wait: Could there be any scenario where the ring moves back to x=0? Let's test small steps: after step1:1, step3:-1, step5:4, step7:-9,... So x coordinate is never 0. So ok.
Thus solution.
Now I'll implement in Python.
Pseudo:
import sys
MOD = 1000000007
memo = {0: (0, 1)}
def fib(n):
if n in memo:
return memo[n]
a, b = fib(n >> 1)
# a = F_k, b = F_{k+1}
t = (2 * b - a) % MOD
c = a * t % MOD # F_{2k}
d = (a * a + b * b) % MOD # F_{2k+1}
if n & 1:
res = (d, (c + d) % MOD)
else:
res = (c, d)
memo[n] = res
return res
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
q = int(next(it))
out_lines = []
for _ in range(q):
N = int(next(it))
k = (N + 1) // 2
f = fib(k)[0]
ans = (f * f) % MOD
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
Now test with sample.
But we must ensure that for N=1, k=1, fib(1) returns (1, something). In fib(1): n=1, check memo? not present. Calls fib(0) via n>>1=0: memo[0] exists -> (0,1). Then a=0,b=1; t=(2-0)%MOD=2; c=0*2=0; d=0+1=1. n odd -> res=(1, (0+1)=1). So memo[1] set to (1,1). Returns (1,1). f = first = 1, ans=1. Good.
Now check N=2: k=1 -> same.
Now N=3: k=2. fib(2): n=2 not in memo. Calls fib(1) -> returns (1,1). a=1,b=1; t=(2-1)=1; c=1*1=1; d=1+1=2; n even -> res=(1,2). memo[2]=(1,2). f=1, ans=1.
N=4: k=2, same.
So output 1 1 1 1.
All good.
Now, consider performance: With Q=10000, each fib(k) could be O(log k). With memo caching across calls, many will be fast. But even without, 10000*30 = 300k recursions, each recursion doing a few arithmetic ops modulo, fine.
But we should be careful: For large numbers like k=500 million, recursion depth is about 30, but each recursive call splits n by 2. In Python, recursion depth is limited, but 30 is safe.
Now consider that we might hit recursion limit if n is huge but depth is log2(n) ~ 30, fine.
Now final.
But wait, there is one more nuance: The identity we used S_M = (-1)^{M-1} F_M^2. This holds when F_1 = 1, F_2 = 1. Is this always true? We can prove by induction. But we have checked for small M.
Now let's ensure that the sum of signed odd-indexed Fibonacci numbers yields that. We can also compute using known identity: F_{n}^2 = F_{n-1} * F_{n+1} + (-1)^{n-1}. Actually Cassini: F_{n-1}F_{n+1} - F_n^2 = (-1)^n. But the alternating sum identity can be derived. However, we are confident.
Nevertheless, we could also compute directly using recurrence for x-coordinate. But better to use the derived formula.
Now what about after even steps? x after even step equals x after previous odd step, so the number of odd steps M = N/2 for even N. So absolute x = (F_{N/2})^2. And for odd N, M = (N+1)/2, absolute x = (F_{(N+1)/2})^2. So both cases covered by ceil(N/2).
Thus answer.
Now I'll produce final solution explanation and code.
But hold on: Did we consider step 0? Not needed.
Now maybe the problem expects the distance as a positive integer, not squared? Since we output squares, it might be large. But sample outputs are small. But that's fine.
Now double-check the constraints: Q up to 10000, N up to 1e9. Our solution fits.
Now check if there is any need to handle negative values mod? No.
Thus final.
But wait, is it possible that the perpendicular distance is not |x| but the Euclidean distance from the ring to the point where the Y-axis meets the path? That doesn't make sense.
Now before finalizing, think about potential pitfalls: The problem description says "Alfi always moves either left or right alternatively, i.e. if she moved left in her last step, she will move right in her current step. Initially she moves right. Roy always moves up or down alternatively, i.e if he moved up in his last step, he will move down in his current step. Initially he moves up." This is clear.
But also: "During Alfi's turn(other than her first step), she sums up her last displacement and Roy's last displacement, and moves the Ring by the value thus obtained." So magnitude = her last displacement (magnitude) + Roy's last displacement (magnitude). So it's sum of magnitudes, not considering direction. So d_n = d_{n-2} + d_{n-1}. Good.
Now might there be any effect of direction on the magnitude? No.
Thus solution stands.
Now code.
We might optimize by computing fib using iterative fast doubling without recursion, but recursion is fine.
One more nuance: The dictionary memo could cause recursion depth if stored many keys, but it's fine.
Now final answer.
Let's also consider the possibility of using matrix exponentiation to compute Fibonacci squared directly: but not needed.
Now I'll craft the explanation for answer.
But the problem statement includes: "Since the result can be very large, print it modulo 1000000007". So we are good.
Now produce final code and explanation.
Now I'm thinking: The identity we used might need to be proved. But for coding it's fine.
Edge Cases:
- N = 1: answer 1
- N = 1000000000 (1e9): k = 500000000, compute fib(500000000) mod 1e9+7, square mod.
Now check that fast doubling works for n=500 million. Recursion depth about 30, fine.
Now potential overflow in intermediate multiplication aa: a can be up to MOD-1, so aa up to ~1e18, Python handles.
Now note: In computing c = a * ((2b - a) % MOD) % MOD, the term (2b - a) % MOD can be computed as (2b - a) % MOD. But if we compute 2b - a directly, it's within Python integer range (since b up to MOD-1, 2*b up to 2e9, minus a, still within 2e9, fine). So no overflow.
Now caching: we store (F_n, F_{n+1}) for each n computed. This may cause memory usage for many distinct n up to maybe a few hundred thousand. That's fine.
Now I think the solution is correct.
One more thing: The problem might be tricky: The perpendicular distance from Y-axis might be measured along the line perpendicular to Y-axis, which is simply |x|. So yes.
But maybe they meant "perpendicular distance from the point to the Y-axis" which is same.
Thus final.
Now consider the possibility that the direction alternation might reset? No.
Now answer.
Now I'll produce final output. ```python
import sys
MOD = 1_000_000_007
memoization: maps n -> (F(n), F(n+1))
memo = {0: (0, 1)}
def fib(n: int):
"""Return (F(n), F(n+1)) modulo MOD using fast doubling."""
if n in memo:
return memo[n]
# recursive call for n//2
a, b = fib(n >> 1) # a = F(k), b = F(k+1) with k = n//2
# formulas for doubling
# F(2k) = F(k) * (2*F(k+1) - F(k))
# F(2k+1) = F(k)^2 + F(k+1)^2
t = (2 * b - a) % MOD
c = a * t % MOD # F(2k)
d = (a * a + b * b) % MOD # F(2k+1)
if n & 1: # n odd: n = 2k+1
res = (d, (c + d) % MOD) # (F(2k+1), F(2k+2))
else: # n even: n = 2k
res = (c, d) # (F(2k), F(2k+1))
memo[n] = res
return res
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
q = int(next(it))
out = []
for _ in range(q):
n = int(next(it))
k = (n + 1) // 2 # ceil(n/2)
f = fib(k)[0] # Fibonacci number F(k)
ans = f * f % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()