We are asked to compute:
S = sum_{i=0}^{n} s_i * a^{n-i} * b^{i} modulo MOD = 1e9+9.
Given n up to 1e9, k up to 1e5. The sequence s_i is periodic with period k, and s_i = +/-1, given first k characters.
We need to compute this huge sum efficiently.
Let's denote term: T_i = s_i * a^{n-i} * b^i.
We can factor a^n: T_i = s_i * a^n * (b/a)^i, but a might not be invertible modulo MOD? Actually we need modulo arithmetic. But we can handle using modular inverse if needed. However, a and b are up to 1e9, MOD is prime 1e9+9, so as long as a mod MOD is not 0, we can invert. But a can be multiple of MOD? MOD=1e9+9 > 1e9, and a ≤ 1e9, so a mod MOD = a (since a < MOD). So a is nonzero mod MOD; same for b. So we can invert safely.
But more efficient: compute sum as a^n * sum_{i=0}^{n} s_i * (b * inv(a))^i. However, we need exponentiation mod MOD.
Define r = b * inv(a) mod MOD. Then S = a^n * sum_{i=0}^{n} s_i * r^i mod MOD.
Now s_i is periodic: s_i = s_{i mod k} for i = 0..n.
Thus we need sum_{i=0}^{n} s_{i mod k} * r^i.
Let N = n. We can group by remainder modulo k.
For each j = 0..k-1, we have indices i = j, j+k, j+2k, ..., up to ≤ n.
Thus sum = sum_{j=0}^{k-1} s_j * sum_{t=0}^{ floor((n-j)/k) } r^{j + t*k} = sum_{j=0}^{k-1} s_j * r^j * sum_{t=0}^{m_j} (r^k)^t
where m_j = floor((n - j) / k) . So number of terms for remainder j is (m_j+1). But careful: if j > n, then none? Since j from 0 to k-1 and n is large, all j <= n? n can be less than k? Possibly n can be less than k? Actually n is up to 1e9, k up to 1e5; n could be less than k. But the condition says "sequence s_0...s_n" and it is k-periodic and k divides n+1. Wait: The problem statement: "Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≤ i ≤ n it's satisfied that s_{i} = s_{i - k}."
Important: They state k divides n+1. So n+1 is multiple of k. That means n+1 = k * q for some integer q, i.e., n = kq - 1. So n >= k-1, so n+1 is multiple of k, thus n >= k-1. So indeed n+1 is divisible by k. That simplifies grouping because for each j, the number of terms will be exactly the same? Let's check: If n = kq - 1, then indices from 0 to kq-1 inclusive. There are total n+1 = kq terms. Since period k, we have exactly q complete cycles? Actually s_i for i=0..k-1 is first period, then for i=k..2k-1 repeats, etc. Since n+1 = k*q, we have exactly q full periods. So there are q terms for each remainder? Check: For remainder j, indices: j, j+k, j+2k, ..., j+(q-1)*k, because j+(q-1)*k ≤ (k-1)+(q-1)k = kq -1 = n. So indeed each remainder j has exactly q terms. So m_j = q-1. So sum over t=0 to q-1 of (r^k)^t * r^j * s_j.
Thus sum = sum_{j=0}^{k-1} s_j * r^j * ( (r^k)^q - 1 ) / (r^k - 1 ), provided r^k != 1 mod MOD. If r^k == 1, then the geometric series sum is q mod MOD.
So S = a^n * sum_{j=0}^{k-1} s_j * r^j * geom, where geom = sum_{t=0}^{q-1} (r^k)^t = if r^k != 1: ( (r^k)^q - 1 ) * inv(r^k - 1); else q.
And note that r = b * inv(a) mod MOD.
But wait: we used a^n factor outside. However, a^n could be computed using fast exponentiation mod MOD. But careful: Since we have factor a^n, we can combine with r^j = (b * inv(a))^j = b^j * a^{-j}. So S = a^n * sum_{j} s_j * b^j * a^{-j} * geom = sum_{j} s_j * b^j * a^{n-j} * geom. Which is consistent.
But we need to compute mod MOD.
Thus algorithm:
Given n, a, b, k, and string signs of length k.
Set MOD = 1_000_000_009 (prime).
Compute q = (n+1) / k. Since n+1 divisible by k, q is integer.
Compute a_mod = a % MOD; b_mod = b % MOD.
Compute r = b_mod * inv(a_mod) % MOD.
Compute r_pow_k = pow_mod(r, k). (exponent k up to 1e5; compute with fast exponent.)
Compute geom:
if r_pow_k == 1:
geom = q % MOD
else:
geom = ( pow_mod(r_pow_k, q) - 1 + MOD ) % MOD * inv( (r_pow_k - 1 + MOD) % MOD ) % MOD
Now compute sum_j = 0 for j=0..k-1: sign_j = 1 if '+' else -1.
term = sign_j * pow_mod(r, j) % MOD; sum_j = (sum_j + term) % MOD.
But careful: We need to incorporate the factor s_j * r^j, but note that the geometric factor is same for all j? Yes, geom is independent of j because it's sum of (r^k)^t over t. But is it exactly the same for all j? Check: The sum for each j is s_j * r^j * geom. Indeed since we have sum_{t=0}^{q-1} (r^k)^t is independent of j. So we can compute total = geom * ( sum_{j=0}^{k-1} s_j * r^j ) % MOD.
Then S = a^n * total % MOD.
But careful: This derivation assumes that r^k != 1? Actually if r^k == 1, then geom = q, and the formula still holds: total = q * sum_{j} s_j * r^j. So it's consistent.
Thus final answer = a_pow_n * total % MOD.
Now we need to compute a_pow_n mod MOD, pow(r, j) for j up to k-1 (k up to 1e5) efficiently. We can compute r_pow_j iteratively in loop.
Complexities: O(k) for the sum of r^j times sign. Also exponentiation for a^n (log n) and for r_pow_k and r_pow_k^q (log q). Since n up to 1e9, q up to n/k which could be up to 1e9/1 ~1e9 but exponent log q is fine.
But we must be careful about integer overflow when computing q = (n+1)/k. n+1 up to 1e9+1, k up to 1e5, q up to 1e9, fits in 64-bit.
Edge Cases:
-
r^k mod MOD could be 1. Then geom = q mod MOD. But we must also be careful if r^k - 1 is 0 mod MOD, then we cannot compute inverse; but we handle with case.
-
If a_mod == 0? But a ∈ [1,1e9], so a_mod = a != 0 mod MOD, since MOD > 1e9. So inv exists.
-
Similarly, r^k - 1 could be 0, but that's handled.
-
The sign factor s_j is ±1, we need to add accordingly: term = (sign == '+' ? 1 : MOD-1) * r_pow_j % MOD? Actually -1 mod MOD is MOD-1. So if sign is '-', term = -r_pow_j mod MOD = (MOD - r_pow_j) % MOD.
Thus we compute sum_r = (sum_r + (sign[j] == '+' ? r_pow : MOD - r_pow)) % MOD.
Now final answer: ans = a_pow_n * ((geom * sum_r) % MOD) % MOD.
Double-check with examples.
Example 1:
n=2, a=2, b=3, k=3, s="+-+". So n+1=3 divisible by 3? yes, q=1.
Compute MOD=1e9+9.
a_mod=2, b_mod=3.
inv(a_mod) mod MOD = inv(2) = (MOD+1)/2? Since MOD=1e9+9, inv(2) = (1e9+9+1)/2 = 500000005? Let's compute: (1e9+9) is odd, inv2 is (MOD+1)/2 = 500000005. So r = 3 * inv2 % MOD = 3 * 500000005 % MOD = 1500000015 mod 1e9+9 = 1500000015 - 1000000009 = 499999006? Actually 1e9+9 = 1000000009. 3*500000005 = 1500000015. 1500000015 - 1000000009 = 500000006. So r = 500000006. That's (3/2) mod MOD.
But r^k = r^3 mod MOD. Compute r^3? Not needed if q=1, geom = sum_{t=0}^{0} (r^k)^t = 1. Since q=1, geom = 1 (if r^k != 1 case? Actually sum_{t=0}^{0} = 1). In our formula, we have geom = if r^k != 1: ((r^k)^q - 1)*inv(r^k-1). But for q=1, (r^k)^1 - 1 = r^k - 1, times inv(r^k-1) = 1. So geom = 1. So fine.
Now sum_r = sum_{j=0}^{2} s_j * r^j. Compute r^0=1, r^1=r=500000006, r^2 = r^2 mod MOD. Let's compute r^2 = (500000006)^2 mod MOD. 500000006^2 = 250000012000000036? Better compute mod: 500000006^2 = (500000006)(500000006) mod 1000000009. Since 500000006 = (MOD+1)/2? Actually MOD=1000000009. (MOD+1)/2 = 500000005, but we have 500000006, so it's (MOD+3)/2? Wait: 2500000006 = 1000000012 = MOD + 3. So r = (MOD+3)/2? Actually (MOD+3)/2 = (1000000009+3)/2 = 1000000012/2 = 500000006. Yes r = (MOD+3)/2. So r ≡ (3)/2 mod MOD. But anyway.
Compute r^2: ((3/2)^2) = 9/4 mod MOD. 9 * inv(4) mod MOD. inv(4) = (MOD+1)/4? Since MOD ≡ 1 mod 4? 1000000009 mod 4 = 1? 1000000008 is divisible by 4? 1000000008/4=250000002, so 1000000009 mod 4 = 1. So inv(4) = (MOD+1)/4? Actually for prime p not dividing a, inv(4) is 4^{p-2} mod p. But can compute: 4250000002 mod 1000000009? 2500000024=1000000008 ≡ -1 mod MOD, so 250000002 is -inv(4)? Actually we need number x such that 4x ≡ 1 mod p. Since 4250000002 = 1000000008 ≡ -1, so 4(-250000002) ≡ 1? But -250000002 mod p = p-250000002 = 750000007? Let's compute: 1000000009 - 250000002 = 750000007. Check 4750000007 = 3000000028 mod p? Let's compute precisely: 7500000074 = 3000000028. 3000000028 - 21000000009 = 3000000028 - 2000000018 = 1000000010, minus 1000000009 = 1. Yes so inv(4) = 750000007. So 9 * inv(4) = 9750000007 = 6750000063. Mod p: 6*1000000009 = 6000000054, subtract: 6750000063 - 6000000054 = 750000009. So 9/4 mod p = 750000009? Not sure.
But easier: Since we just need the final answer mod p = 7. Let's compute with our formula: a_pow_n = 2^2=4. sum_r: s0=+ => +1; s1='-'=> -r; s2='+'=> +r^2. So total = 1 - r + r^2.
Compute r = (3 * inv(2)) mod p = 500000006 as above. r^2 = (3^2 * inv(2)^2) = 9 * inv(4) = 9 * 750000007 mod p = 6750000063 mod p. Let's compute mod: 1000000009*6 = 6000000054, remainder = 750000009. So r^2 = 750000009. Then 1 - r + r^2 = 1 - 500000006 + 750000009 = (1 + 750000009) - 500000006 = 750000010 - 500000006 = 250000004. mod p? That's less than p. So total = 250000004. Multiply by a_pow_n=4: 4 * 250000004 = 1000000016. Mod p: 1000000016 - 1000000009 = 7. Good.
So formula works.
Example 2: n=4, a=1, b=5, k=1, s='-'. So n+1=5, k=1, q=5. a_mod=1, inv(a_mod)=1, r = 5. r^k = 5^1=5, r_pow_k != 1. geom = (5^5 - 1) * inv(5-1) = (3125 - 1) * inv(4) = 3124 * inv(4). inv(4) mod p = 750000007 as computed. 3124 * 750000007 mod p: Let's compute 3124750000007 = 3124 * 750000007 = 7500000073000=2,250,000,021,000; plus 750000007124=93,000,000,868; total = 2,343,000,021,868? Actually careful: 750000007 * 3124 = 750000007 * (3000 + 124) = 2,250,000,021,000 + (750000007124). Compute 750,000,007*100=75,000,000,700; *20=15,000,000,140; *4=3,000,000,028; sum = 75,000,000,700+15,000,000,140=90,000,000,840; +3,000,000,028=93,000,000,868. So total = 2,250,000,021,000 + 93,000,000,868 = 2,343,000,021,868. Now mod 1,000,000,009. We can compute remainder: 1,000,000,009 * 2343 =? 1,000,000,009 * 2000 = 2,000,000,018,000; *300 = 300,000,002,700; *40 = 40,000,000,360; *3 = 3,000,000,027; sum = 2,000,000,018,000+300,000,002,700=2,300,000,020,700; +40,000,000,360=2,340,000,021,060; +3,000,000,027=2,343,000,021,087. Actually product = 2,343,000,021,087. Our number is 2,343,000,021,868. Subtract: 2,343,000,021,868 - 2,343,000,021,087 = 781. So geom = 781 mod MOD.
Now sum_r: j from 0 only, s0 = -1, r^0=1 => sum_r = -1 mod MOD = MOD-1 = 1000000008. So total = geom * sum_r mod MOD = 781 * 1000000008 mod MOD. 781 * 1000000008 = 781 * (1000000000+8) = 781,000,000,000 + 6,248 = 781,000,006,248? Actually 7818=6,248. So 781,000,006,248. Now mod MOD: Divide by 1,000,000,009 * 781 =? 1,000,000,009 * 781 = 1,000,000,009700=700,000,006,300; 80=80,000,000,720; 1=1,000,000,009; sum = 700,000,006,300+80,000,000,720=780,000,007,020; +1,000,000,009=781,000,007,029. Our number 781,000,006,248 is less than that? Actually 781,000,007,029 > 781,000,006,248, so remainder = 781,000,006,248 - (781,000,007,029 - MOD? ) Wait we need to subtract multiples: Let's compute 1,000,000,009 * 781 = 781,000,007,029. Our product is 781,000,006,248. Subtract: 781,000,007,029 - 781,000,006,248 = 781. So product = 781,000,007,029 - 781. So it is less by 781, i.e., product = 781(MOD) - 781? Actually 781 * MOD = 781,000,007,029. Our product is 781 (MOD-1) = 781*MOD - 781 = 781,000,007,029 - 781 = 781,000,006,248. So product mod MOD = -781 mod MOD = MOD - 781 = 1000000009 - 781 = 999999228. That's the total? Actually total = geom * sum_r = 781 * (MOD-1) = -781 mod MOD. Then S = a_pow_n * total, with a_pow_n = 1^4 = 1. So answer = 999999228, which matches example. Good.
Thus formula works.
However, we must be careful: There is an alternative approach if we don't want to use inversion due to large exponents but it's fine.
Potential pitfalls: When r^k = 1, geom = q mod MOD. But also note that in that case, sum_{t=0}^{q-1} (r^k)^t = q, but if q is huge (up to 1e9) we can compute q % MOD directly.
But also note: When r^k = 1, the expression (r^k)^q = 1^q = 1, so (r^k)^q - 1 = 0; we cannot use inverse. So we treat separately.
Edge: what if r^k = 1 and also r^k - 1 = 0, but also the denominator is zero, as handled.
But there is another nuance: r might be 0? Could r = 0 mod MOD? r = b * inv(a). Since a is not 0, inv(a) exists and non-zero. b could be 0? But b is at least 1, so b mod MOD = b, nonzero. So r != 0. Good.
Now what about a^n factor? Since a < MOD, a_mod = a; a^n mod MOD we compute using fast exponent. n up to 1e9, log2(1e9) ~ 30, fine.
We also need to compute r_pow_k ^ q mod MOD. q up to 1e9, fine.
But we also need to compute sum_{j=0}^{k-1} s_j * r^j mod MOD. k up to 1e5, compute r_pow iteratively: start pow=1, loop j=0..k-1, sum += sign_j * pow, pow = pow * r % MOD.
Alternatively, compute r_pow using fast exponent each iteration? No, O(k) is fine.
Thus overall O(k + log n + log q). Good.
But wait: There is a nuance: The derivation assumed that n+1 is divisible by k. The problem explicitly says k divides n+1. So we are safe.
But what about if q = 0? Since n>=0 and k divides n+1, n+1>=k? Actually k>=1, so n+1>=k, so q>=1. So fine.
Now, we must be careful with negative modulo handling: In sum_r, if sign is '-', we add MOD - r_pow.
Now test more edge cases: Suppose k=1, s='+', then sum_r = 1 * 1 = 1. Then S = a^n * geom, where geom = sum_{t=0}^{q-1} r^t (since r^k = r). Actually formula: S = a^n * (geom * sum_r) with sum_r = s0 * r^0 = 1. So S = a^n * geom, where geom = sum_{t=0}^{q-1} (r)^t. That matches sum_{i=0}^{n} a^{n-i} b^i = a^n sum_{i=0}^{n} (b/a)^i, which is geometric series. Good.
Now confirm that our factor sum_r indeed is sum over one period of s_j * r^j.
But wait: We used the formula S = a^n * sum_{j} s_j * r^j * sum_{t=0}^{q-1} (r^k)^t. However, careful: The sum over j and t: r^{j + t*k} = r^j * (r^k)^t. Since q terms for each j, it's exactly that product. So yes.
Now about modular arithmetic: All operations mod MOD.
One more nuance: In computing geom when r^k != 1, we compute inv(r^k - 1). But r^k - 1 may be negative mod, so we add MOD before taking inverse.
Similarly, (r_pow_k)^q - 1 may be negative, add MOD.
Now implement.
Potential overflow: Use 64-bit integers (long long). Multiply and mod.
Now, is there any hidden trick? The modulus is 1e9+9, which is prime. So exponentiation mod is fine.
Now double-check if there are any off-by-one issues: Since i from 0 to n inclusive, we have n+1 terms. Since n+1 = k*q, we have q terms per remainder. Indeed.
Now, we need to read input: first line n, a, b, k. Second line a string of length k consisting of '+' and '-'. Possibly there may be newline.
Now output integer.
Now consider large k up to 100k, loops fine.
Now let's test a simple case: n=0, k divides 1? k must divide 1, so k=1. n=0, a, b arbitrary. Then q = (0+1)/1 =1. s given length 1. Then S = s0 * a^{0} * b^{0} = s0 * 1. Our formula: r = b * inv(a); a_pow_n = a^0 =1; sum_r = s0 * 1; geom = sum_{t=0}^{0} (r^k)^t = 1. So total = s0, times a^n =1 => s0. Good.
But wait: If n=0, n+1=1 must be divisible by k, so k can be 1. Good.
Now consider n=3, k=2? Then n+1=4, divisible by 2, q=2. So indices: 0,1,2,3. s periodic with period 2: s0, s1, s2=s0, s3=s1. So our grouping: j=0: indices 0,2 => two terms; j=1: indices 1,3 => two terms. Works.
Now, if we attempted to compute sum_{j} s_j r^j * geom with geom = 1 + r^k. Since q=2, sum_{t=0}^{1} (r^k)^t = 1 + r^k. So S = a^3 * [ s0 * r^0 * (1+r^k) + s1 * r^1 * (1+r^k) ] = a^3 * (1+r^k) * (s0 + s1 r). But is that equal to sum_{i=0}^{3} s_i a^{3-i} b^i? Let's test numerically: a=1, b=2, s0=1, s1=-1. Then S = 1^3 * (1 + (2)^2? Actually r = b/a =2, r^k = 2^2=4, geom=1+4=5. sum_r = s0 + s1 r = 1 - 2 = -1. So product = -5. Compute directly: s0 a^3 b^0 =1; s1 a^2 b^1 = -2; s2 = s0 a^1 b^2 = 4; s3 = s1 a^0 b^3 = -8; sum = 1-2+4-8 = -5. Yes.
So formula holds.
Now implement.
However, there is another nuance: The exponent n can be up to 10^9, and we compute a^n mod MOD using exponentiation which is O(log n). Fine.
But what about q = (n+1)/k. n+1 up to 10^9+1, q up to 10^9+1. Exponentiating r_pow_k to q uses O(log q). That's fine.
Potential problem: When computing r_pow_k, we do pow_mod(r, k). k up to 10^5, O(log k). Fine.
Now, one more nuance: The problem states that s_i are integers 1 or -1. It also says "It's known that sequence is k-periodic and k divides n+1." So we rely on that.
But there might be tricky part: Since n is up to 10^9, but k is up to 10^5, q = (n+1)/k may be up to 10^9. That's okay.
Now we need to be careful about potential large numbers: The exponentiation may be done with powmod functions using long long.
Now, implement.
Implementation steps:
- MOD = 1000000009
- Read n, a, b, k.
- Read string s of length k.
- Compute q = (n + 1) / k. Since integer division, check that (n+1) % k == 0? But input guarantee, but still safe.
- Compute a_mod = a % MOD; b_mod = b % MOD.
- Compute inv_a = modinv(a_mod) using Fermat: powmod(a_mod, MOD-2, MOD).
- r = b_mod * inv_a % MOD.
- Compute r_pow_k = powmod(r, k, MOD).
- Compute geom:
if r_pow_k == 1:
geom = q % MOD
else:
numerator = (powmod(r_pow_k, q, MOD) - 1 + MOD) % MOD
denominator = (r_pow_k - 1 + MOD) % MOD
geom = numerator * modinv(denominator) % MOD
- Compute sum_r = 0.
cur = 1 (r^0)
for j from 0 to k-1:
if s[j] == '+': sum_r = (sum_r + cur) % MOD
else: sum_r = (sum_r + MOD - cur) % MOD
cur = cur * r % MOD
- total = geom * sum_r % MOD.
- a_pow_n = powmod(a_mod, n, MOD).
- ans = a_pow_n * total % MOD.
- Print ans.
We should also consider the possibility that we could compute sum_r directly as sum_{j} s_j * r^j, where r^j computed via iterative multiplication. That's correct.
Now check complexities: k up to 100k, loop O(k) fine.
Now testing on given examples: works.
Now what about potential overflow on multiplication in powmod? Use (long long) and mod.
Now check edge case: r_pow_k == 1 and q mod MOD may be zero? But geom = q % MOD; that's fine.
Now consider r^k == 1 but sum_r may be 0? That would cause total = 0, so answer = 0? That's possible.
Now, is there any other subtlety about the exponent? Because the exponent n-i and i can be large, but we handled.
Now I think solution is correct.
But wait: Could there be an issue with using modular inverse of a? Since a is less than MOD, it's fine. But suppose a is a multiple of MOD? Not possible as a <= 1e9 < MOD.
Now double-check the modulus: 1e9+9 is prime? Yes, 1000000009 is a prime. So Fermat's little theorem holds.
Now let's think about alternative if we didn't factor out a^n: Might have to compute geometric series with factor b/a. Using inversion is fine.
Now, one more nuance: Since we have large exponents, the powmod(r_pow_k, q) might require q up to 1e9. That's okay.
Now confirm with random tests:
Let n=9, a=2, b=3, k=5. Check n+1=10, not divisible by 5? Actually 10/5=2, yes divisible. So q=2.
s random: "+-+--". Compute manually to ensure.
But trust.
Now, there is a nuance: The sum_{i=0}^{n} s_i a^{n-i} b^{i} can be thought as polynomial evaluation. But we already did.
Now I will produce final answer.
But wait: I recall something about "non-negative remainder" which is standard, our ans mod MOD already yields non-negative.
Now, is there any trick with large k? 100k is fine.
Now we need to code in C++ perhaps, but answer should be algorithm.
Now let's reflect on a potential pitfall: The periodic condition says for each k ≤ i ≤ n, s_i = s_{i - k}. Since k divides n+1, we have exactly q full periods. However, what if n+1 is not a multiple of k? The statement says it is, so it's guaranteed.
But what if k=0? k is at least 1.
Now, I'm comfortable.
Now produce final solution explanation.
However, I recall that there might be a possibility to avoid using modular inverses by handling as a geometric series over the whole sum but we already did.
Now final answer.
But let's think if there is any issue with r^k - 1 when r^k mod MOD = 1 but also denominator = 0, and we handle as separate case.
Now, what about negative signs? Already handled.
Now, I'll write the solution in a clear way.
One more nuance: The exponent n can be 0, so a^0=1.
Now code.
Edge case: If q is huge, powmod(r_pow_k, q) is fine.
Now we need to implement modinv using powmod.
Alright.
Now I will produce final solution.
But before finalizing, double-check with a scenario where r^k mod MOD = 1, but q is also MOD multiple, then geom = q % MOD = 0, total = 0? That could be correct if sum_r is not 0, but the sum of geometric series mod MOD might be 0 if q mod MOD = 0. However, is that consistent with actual sum? Since actual sum (over integers) is q, but q mod MOD is 0. Since we are computing modulo MOD, that's correct.
Now, if r^k = 1 and q is multiple of MOD, the sum of q terms of 1 is q, and mod MOD that is 0. So geom = 0.
Now, but what if r^k = 1 and sum_r is non-zero, then total = 0. Could that be correct? Let's test: Suppose a=1,b=1 => r=1, then r^k =1, geom = q mod MOD. The actual sum S = sum_{i=0}^{n} s_i * 1^{n-i} * 1^{i} = sum_{i=0}^{n} s_i. Since s_i is periodic, sum over full periods: total = q * sum_{j=0}^{k-1} s_j. So S = q * sum_period. So modulo MOD, S mod MOD = (q mod MOD) * (sum_period mod MOD) mod MOD. So indeed if q mod MOD = 0, S mod MOD = 0 regardless of sum_period (unless sum_period also 0 mod MOD, but anyway). So our formula gives total = geom * sum_r = (q mod MOD) * sum_r. Since geom = q % MOD, that's correct. So fine.
Now, we need to verify that our handling of sum_r is sum_period mod MOD: sum_{j=0}^{k-1} s_j * r^j. Wait, when r=1, that's sum_{j=0}^{k-1} s_j, which is sum_period. So fine.
Now everything consistent.
Now we need to note: When r^k != 1, the factor geom = ( (r^k)^q - 1) / (r^k - 1). But we multiply by sum_r. So total = sum_r * geom. That yields sum_{j} s_j r^j * (1 - (r^k)^q)/(1 - r^k). But careful: geometric series: sum_{t=0}^{q-1} x^t = (1 - x^q)/(1 - x). So our numerator is (r_pow_k)^q - 1, denominator r_pow_k - 1. So it's the same as (1 - (r_pow_k)^q) / (1 - r_pow_k) but sign difference: Actually (1 - x^q)/(1-x) = (x^q - 1)/(x - 1). Since we have numerator (x^q - 1) and denominator (x - 1), that is same but multiplied by -1? Wait: (x^q - 1)/(x - 1) = (1 - x^q)/(1 - x). Yes because denominator multiply by -1, numerator multiply by -1, same ratio. So it's fine.
Now ensure we use (r_pow_k)^q - 1, and denominator r_pow_k - 1.
Now, be careful with mod subtraction: (r_pow_k - 1) could be negative mod, so add MOD.
Now, we might need to compute powmod(r_pow_k, q, MOD). Since q up to 1e9, fast exponent.
Now, we might precompute r_pow_j? Already loop.
Now, I'd also like to think about potential overflow on multiplication of two numbers up to MOD-1, product up to ~1e18, fits in 64-bit (since MOD ~1e9, product ~1e18 < 2^63-1 ~9e18). So long long is safe. In C++ we can use long long.
Now, we can finalize.
But I recall that the problem might be from Codeforces (CF 963? Actually I remember problem 1195C? No, 300C? There's a known problem "Find the Sum of the Expression" with modulo 1e9+9. Actually this is Codeforces Round #313? But anyway.
Now, is there a need to treat a^n in a separate way if a is 0? But a is at least 1, so fine.
Now, what if a is multiple of MOD? Not possible.
Now, I'll provide final answer.
But we should consider possible optimization: If k is large, computing sum_r O(k) is fine.
Now code.
But one more nuance: The input string length is k. It might include newline etc. Use getline or cin.
Now, we need to handle that n is up to 1e9, which fits in 32-bit? 1e9 < 2^31, but n+1 up to 1e9+1, fits in 32-bit signed? 1e9+1 = 1000000001 < 2^31-1=2147483647, so int works, but using long long to avoid overflow in multiplication maybe. But q = (n+1)/k, n+1 up to 1e9+1, k up to 1e5, q up to 1e4? Actually n+1 up to 1e9+1, so q up to ~1e9, which doesn't fit in 32-bit? 1e9 fits in 32-bit (max 2.147e9). So 1e9 fits. So int can hold up to 2.147e9, safe. But in C++ int is typically 32-bit, so ok. However, for safety, use long long.
Now, for exponentiation, the exponent is n (up to 1e9) and q (up to 1e9), so int is enough, but we can use long long.
Now implement.
Now, let's double-check the modulo value: 1e9+9 = 1000000009. I recall sometimes 1e9+7 and 1e9+9 are both primes. Yes.
Now, we produce final answer.
But wait: In the problem statement they said "sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1." In input they give a sequence of length k (characters). So that's correct.
Now I'll produce final explanation and code.
But before final, test with some random small values via mental or small script? Let's test a few manually:
Case: n=3, a=1, b=2, k=2, s='++' => all s_i =1. Then S = sum_{i=0}^{3} 1^{3-i} 2^i = 2^0 + 2^1 + 2^2 + 2^3 = 1+2+4+8=15.
Our method: q = (3+1)/2=2, a_mod=1, inv_a=1, r=2, r_pow_k = 2^2=4, geom = (4^2-1)/(4-1) = (16-1)/3=15/3=5? Wait compute: (r_pow_k)^q = 4^2=16; numerator 16-1=15; denominator 4-1=3; geom=5. sum_r: j=0: s0=+ => +1; j=1: s1=+ => +r=2; sum_r=3. total = 5*3=15. a_pow_n=1^3=1. ans=15. Good.
Case: n=3, a=2, b=3, k=2, s='+-' (i.e., s0=1, s1=-1, then s2=1, s3=-1). Compute directly: terms: i=0: 2^33^0=8; i=1: -2^23^1=-12; i=2: +2^13^2=+18; i=3: -2^03^3=-27; sum=8-12+18-27=-13. Mod: 1000000009-13=999999996.
Our method: q=2, a=2, b=3. inv_a = inv(2)=500000005, r = 3inv_a mod MOD = 500000006. r_pow_k = r^2 = (500000006)^2 mod MOD. Previously we computed r^2 = 750000009? Actually earlier we had r = 500000006, r^2 = 750000009? Let's recompute: r = 500000006. Compute r^2: (500000006)^2 = (500000006 * 500000006). 500000006 * 500000006 = 250000012000000036? Let's compute mod: Since r = (3inv(2)) mod MOD, and inv(2)=500000005? Wait earlier we computed inv(2) = 500000005? Let's double-check: MOD=1000000009, 2500000005 = 1000000010, mod = 1? Actually 1000000010 mod 1000000009 = 1. So inv(2) is 500000005. Indeed 2500000005 = 1000000010 ≡ 1 mod MOD. So inv(2)=500000005. Then r = b * inv(a) = 3 * 500000005 = 1500000015 mod MOD = 1500000015 - 1000000009 = 500000006. So correct.
Now r^2 = (500000006)^2 =? Compute (5e8+6)^2 = 2.5e17? Better mod: Use representation: r = (MOD+3)/2, as earlier. So r = (MOD+3)/2, thus r^2 = (MOD+3)^2 / 4 = (MOD^2 + 6MOD + 9)/4. Mod MOD, MOD^2 ≡ 0, 6MOD ≡ 0, so r^2 ≡ 9/4 mod MOD. 9 * inv(4) mod MOD = 9 * 750000007 = 6750000063 mod MOD = 6750000063 - 6*1000000009=6750000063-6000000054=750000009. So r^2 = 750000009. So r_pow_k = r^2 = 750000009.
Now geom: (r_pow_k)^q = (750000009)^2 mod MOD. Compute square: 750000009^2 =? 750000009 * 750000009. Since 750000009 = MOD - 250000000? Actually MOD=1000000009, 1000000009 - 250000000 = 750000009? Wait 1000000009 - 250000000 = 750000009, yes. So 750000009 = -250000000 mod MOD. So (750000009)^2 ≡ (-250000000)^2 = 62500000000000000 mod MOD. But we can compute: 250000000^2 = 6.25e16? Let's compute modulo more carefully later. But we can compute geom numerically? Actually we could compute answer directly from our formula: sum_r = s0 * r^0 + s1 * r^1 = 1 + (-1)*r = 1 - r = 1 - 500000006 = -500000005 mod MOD = MOD - 500000005 = 500000004? Wait 1 - 500000006 = -500000005, mod MOD = 1000000009 - 500000005 = 500000004. So sum_r = 500000004.
Now geom = ( (r_pow_k)^q - 1 ) * inv(r_pow_k - 1). r_pow_k - 1 = 750000009 - 1 = 750000008. r_pow_k - 1 mod MOD is 750000008. inv? not trivial. But we can compute geom manually by using rational: Since q=2, geom = 1 + r_pow_k = 1 + 750000009 = 750000010 mod MOD? Actually sum_{t=0}^{1} (r^k)^t = 1 + r^k = 1 + 750000009 = 750000010. So geom should be 750000010. Because q=2 => geom = 1 + r^k. That's simpler than using formula. So geom = 750000010. Let's verify via formula: (r^k)^2 - 1 = (750000009^2 - 1). And r^k - 1 = 750000008. If we compute (750000009^2 - 1) / (750000009 - 1) = ( (750000009-1)(750000009+1) ) / (750000009-1) = 750000010. Yes. So geom = 750000010.
Now total = geom * sum_r = 750000010 * 500000004 mod MOD. Compute: 750000010 * 500000004 = ? But we expect answer = -13 mod MOD = 999999996. Let's compute: 750000010 * 500000004 = 750000010 * (5e8+4) = 750000010500000000 + 7500000104. 750000010*500000000 = 375000005000000000? Actually 750000010 * 5e8 = 3.75e17? This is messy. But mod we can trust.
We'll compute mod using properties: Since we suspect answer should be -13 mod MOD, let's compute simpler: S = a^n * total. a^n = 2^3 = 8 mod MOD. So total should be such that 8 * total ≡ -13 mod MOD => total ≡ -13 * inv(8) mod MOD. inv(8) mod MOD: 8 * 125000001 =? 1250000018=1000000008 ≡ -1 mod MOD, so -125000001 8 ≡1? Actually we need x s.t. 8x ≡ 1 mod MOD. Since 8125000001=1000000008 ≡ -1, so 8(-125000001)≡1 => -125000001 mod MOD = MOD-125000001 = 875000008? Wait 1000000009 - 125000001 = 875000008. So inv(8) = 125000001? Check 8*125000001 = 1000000008, which is -1 mod MOD, not 1. So we need 8 * y = 1 => y = 8^{MOD-2}. But let's compute actual inv(8) mod 1e9+9: Using Fermat: 8^{1000000007} mod MOD. But we can compute by solving 8x ≡ 1 mod MOD. Since 8 * 125000001 = 1000000008 ≡ -1, so 8 * (-125000001) ≡ 1 => -125000001 mod MOD = MOD - 125000001 = 875000008. So inv(8) = 875000008? Check: 8 * 875000008 = 7,000,000,064? Compute: 875,000,008 * 8 = 7,000,000,064. Now 7,000,000,064 mod 1,000,000,009: 1,000,000,009 * 7 = 7,000,000,063. So 7,000,000,064 - 7,000,000,063 = 1. Yes! So inv(8)=875000008. Good.
So total expected = (-13) * 875000008 mod MOD. Compute -13875000008 = -11375000104 mod MOD. 11375000104 / MOD = 11? MOD=1e9+9=1000000009. 100000000911=11000000099. Subtract: 11375000104 - 11000000099 = 375000005. So -11375000104 mod = MOD - (11375000104 - 11*MOD) ? Actually we have negative value: -11375000104 mod MOD = MOD - (11375000104 mod MOD)? Let's do properly: total = (-13)*inv(8) mod MOD. Since -13 ≡ MOD-13 = 999999996. Multiply: 999999996 * 875000008 mod MOD. Might be messy. But we don't need to compute.
But from earlier direct sum we got answer = -13 mod MOD = 999999996. So S = 999999996.
Now compute total = geom * sum_r = 750000010 * 500000004 mod MOD. Let's compute that product mod MOD: We can do: 750000010 * 500000004 =? Use splitting: 750000010 = 75e6? Actually we can compute mod using Python mental? Let's compute:
750000010 * 500000004 = 750000010 * (500000000 + 4) = 750000010500000000 + 7500000104.
750000010*500000000 = 750000010 * 5e8 = (750000010 * 5) * 10^8? Actually 750000010 * 500,000,000 = 750000010 * 5 * 100,000,000 = 3,750,000,050 * 100,000,000? Better: 750000010 * 5e8 = 750000010 * 500,000,000 = 375,000,005,000,000,000? Let's compute precisely: 750,000,010 * 500,000,000 = 750,000,010 * 5 * 100,000,000 = 3,750,000,050 * 100,000,000 = 375,000,005,000,000,000? Actually 3,750,000,050 * 100,000,000 = 375,000,005,000,000,000. Yes.
Then plus 750,000,010 * 4 = 3,000,000,040.
So total product = 375,000,005,000,000,000 + 3,000,000,040 = 375,000,008,000,000,040? Wait: 375,000,005,000,000,000 + 3,000,000,040 = 375,000,008,000,000,040? No: 375,000,005,000,000,000 + 3,000,000,040 = 375,000,008,000,000,040? That seems off. Actually adding 3,000,000,040 to the last part: the number is 375,000,005,000,000,000, the last digits: ...005,000,000,000 + 3,000,000,040 = ...008,000,000,040. So yes.
Now mod MOD = 1000000009. We need remainder. Compute division by 1000000009. That's complicated. But we trust.
Alternatively, note that total = geom * sum_r = (1 + r^k) * (1 - r) = (1 + r^2)(1 - r). With r=500000006, r^2=750000009. So (1 + 750000009) = 750000010, (1 - 500000006) = -500000005 mod MOD = 500000004. So product = 750000010 * 500000004 mod MOD. Then multiply by a^n=8. So S = 8 * product mod MOD. We can compute product easily by noting that S = a^n * ( (1 + r^k) * (1 - r) ) = 8 * ( (1+r^2)(1-r) ). But we could compute directly: 1+r^2 = 1+9/4 = (4+9)/4 =13/4? Wait since r = 3/2 mod MOD, r^2 = 9/4, so 1+r^2 = 1 + 9/4 = 13/4. And 1 - r = 1 - 3/2 = -1/2. So product (1+r^2)(1-r) = (13/4)(-1/2) = -13/8. Then total = a^n * (-13/8) = 8 * (-13/8) = -13. So indeed S = -13. So the algebra works out symbolically. So mod computation should yield -13 mod MOD = 999999996. So good.
Thus our formula works fine.
Now, consider one more edge: if r^k = 1, then geom = q mod MOD. But what if r^k = 1 and q is huge? No problem.
Now, about computing inv(r^k - 1): we need modular inverse. Since MOD is prime and r^k - 1 not zero (since we are in else case), inverse exists.
But one more nuance: r^k - 1 may be negative, we add MOD before inversion.
Now, there is a potential risk: we compute sum_r using loop with cur = cur * r % MOD, and r might be 0? But r is never 0 as argued.
Now, we must also be careful about possible integer overflow in multiplication: cur = cur * r % MOD. Since cur and r < MOD, product < 1e18, fits in long long. So we can do: cur = (cur * r) % MOD.
Now, we need to read the string: It may be provided without spaces; use cin >> s.
Now, we need to ensure that we treat the input properly: The first line contains n, a, b, k. The second line contains a sequence of length k consisting of '+' and '-'. Ensure we read it as a string. Since k up to 1e5, the string length is exactly k. So we can read with cin.
Now, we might need to consider that n can be as large as 10^9, so storing n+1 as long long is fine.
Now, implementation details for modinv: We can write a function powmod(base, exp, MOD) using iterative method.
Now, check if there is any need to handle negative modulo at final step: final answer after multiplication may exceed MOD, we mod it.
Now, I'll produce final solution.
But wait: There is another approach: Since the expression looks like (a+b) style but with signs. But not needed.
Now, I'll think about potential pitfalls for large exponents: powmod for base = r_pow_k, exponent = q. q can be as large as 1e9, which is okay with fast exponent O(log q). However, one must be careful that the exponent might be 0? q >=1, so fine.
Now, check for scenario when r_pow_k = 0? Not possible because r != 0 and k>0, so r_pow_k != 0.
Now, all good.
Now, I'll draft the final answer explanation: We derive closed form using geometric series.
Potential alternative: Could there be a case where we need to compute a^n modulo MOD after having multiplied by inverse? Already we did.
Now, time to write final answer.
But there is a nuance: The expression sum_{i=0}^{n} s_i a^{n-i} b^{i} can also be seen as a^k * sum_{...} maybe, but our method is fine.
Now, answer.
Wait, but what if a mod MOD = 0? a is at least 1 and less than MOD, so a mod MOD = a, nonzero. So safe.
But could b mod MOD = 0? b <= 1e9 < MOD, so no.
Thus all inverses exist.
Now, final code.
But let's consider if the input guarantees that n+1 is divisible by k. The problem statement says "It's known that sequence is k-periodic and k divides n+1." So we rely on that.
But what if they don't enforce that in the input? It says "It's known that..." So it's guaranteed. So we don't need to check.
Now, the sum we computed uses this fact for grouping. But suppose we didn't rely on it? We would need to handle remainder when n+1 not multiple of k. But it is.
Thus solution stands.
Now, I'll write final answer.
But I should also consider if there is any possibility that using the modular inverse of a is problematic because a may be 0 mod MOD? Since a <= 1e9 < MOD, a mod MOD = a, non-zero. So safe.
Now, check the second example: a=1, b=5, r=5. In our code, we compute inv(a) = 1, r=5. Good.
Now, we need to make sure to use long long for all variables that may be involved in multiplication before mod.
Now, final answer.
Potential issues: When computing q = (n+1)/k, n+1 might exceed 32-bit? n up to 1e9, n+1 up to 1e9+1, fits in 32-bit signed? 1e9+1 = 1000000001 < 2^31-1 = 2147483647, so int works but to avoid overflow in later multiplication (q mod MOD) we use int or long long. However, if we compute geom = q % MOD, we need q modulo MOD; since q may be up to 1e9, it's fine.
Now, we should also consider that powmod(r_pow_k, q) uses exponent q as long long. In C++, powmod function often uses exponent as int or long long; we can use long long.
Now, implement.
Now, I'll produce the final answer.
But wait: Could there be a scenario where r^k mod MOD = 1 and q is 0? Not possible.
Now, I think that's it.
Now, I'll provide the solution in textual explanation and code.
But the user asked: "You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≤ i ≤ n it's satisfied that s_{i} = s_{i - k}. Find out the non-negative remainder of division of ∑ {i=0}^{n} s{i} a^{n - i} b^{i} by 10^{9} + 9." So I'll answer with solution.
Thus final answer: We'll output one integer.
Now, I'll write a C++ code.
Pseudo-code:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000009;
long long modpow(long long base, long long exp) {
long long result = 1;
while (exp > 0) {
if (exp & 1) result = result * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return result;
}
long long modinv(long long x) {
return modpow(x, MOD-2);
}
int main() {
long long n, a, b, k;
cin >> n >> a >> b >> k;
string s;
cin >> s;
long long q = (n + 1) / k; // integer division
a %= MOD;
b %= MOD;
long long inv_a = modinv(a);
long long r = b * inv_a % MOD;
long long r_pow_k = modpow(r, k);
long long geom;
if (r_pow_k == 1) {
geom = q % MOD;
} else {
long long numerator = (modpow(r_pow_k, q) - 1 + MOD) % MOD;
long long denominator = (r_pow_k - 1 + MOD) % MOD;
geom = numerator * modinv(denominator) % MOD;
}
long long sum_r = 0;
long long cur = 1;
for (int i = 0; i < k; ++i) {
if (s[i] == '+') {
sum_r = (sum_r + cur) % MOD;
} else {
sum_r = (sum_r + MOD - cur) % MOD;
}
cur = cur * r % MOD;
}
long long total = geom * sum_r % MOD;
long long a_pow_n = modpow(a, n);
long long ans = a_pow_n * total % MOD;
cout << ans << endl;
return 0;
}
But careful: When a = 0? Not possible. However, there is an edge: if a_mod = 0? Not as argued.
But note: The above code uses inv(a) which is fine.
Now, check for correctness: a_pow_n = modpow(a, n). But a may be 0 mod MOD? No.
Now, we might need to use long long for n because modpow takes exponent as long long.
Now, test with examples:
Example 1: Input:
2 2 3 3
+-+
Compute: n=2, a=2, b=3, k=3. q = (2+1)/3 = 1. r = 3 * inv(2) = 500000006. r_pow_k = r^3? Actually k=3, r^3 =? Not needed if q=1, geom = 1 (since r_pow_k != 1? Actually r_pow_k might not be 1; but we use else case: numerator = (r_pow_k^1 - 1) = r_pow_k -1; denominator = r_pow_k -1; geom =1; works). sum_r: loop: i=0 '+': +1; i=1 '-': MOD- cur (cur = r = 500000006) => sum_r = 1 + MOD - 500000006 = 500000004? Actually 1 + MOD - 500000006 = 1 + 1000000009 - 500000006 = 500000004. i=2 '+': cur now = r^2 = 750000009, sum_r = 500000004 + 750000009 = 1250000013 mod MOD = 250000004? Since 1250000013 - 1000000009 = 250000004. So sum_r = 250000004. total = geom * sum_r = 1 * 250000004 = 250000004. a_pow_n = 2^2=4. ans=4*250000004=1000000016 mod MOD = 7. Good.
Example 2: 4 1 5 1
- . n=4, a=1,b=5,k=1, q=5. r =5inv(1)=5. r_pow_k = 5^1=5. r_pow_k !=1, so geom = (5^5-1)/(5-1) = (3125-1)/4 =3124/4=781? mod MOD. numerator = 3125? Actually powmod(5,5)=3125 mod MOD =3125. numerator = 3125-1=3124. denominator=4. inv(4)=750000007? Actually compute inv(4) via modpow: 4^{MOD-2} mod MOD = 750000007 (as earlier). So geom = 3124750000007 % MOD. But 3124750000007 mod MOD = 781? Let's compute: 3124/4 =781, but we do mod: Since denominator 4 invertible, product yields 781 mod MOD. So geom = 781. sum_r: k=1, s[0]='-', so sum_r = MOD - 1 = 1000000008. total = 781 * 1000000008 % MOD = -781 mod MOD = 999999228? Actually 7811000000008 = 781*(MOD - 1) = 781*MOD - 781 ≡ -781 mod MOD = MOD-781 = 1000000009-781 = 999999228. a_pow_n = 1^4=1. ans = 999999228. Good.
Thus code works.
Now, we should be mindful of time: O(k) which is up to 1e5; powmod calls: inv(a) uses powmod; r_pow_k = powmod(r,k); maybe another powmod for numerator if r_pow_k != 1. Also a^n powmod. So total about 5 powmod calls each O(log exponent) ~ 30 steps, negligible.
Now, we need to consider if k is large (1e5) but computing r_pow_j loop O(k) fine.
Now, one more nuance: The problem statement says n can be up to 10^9, but also maybe n+1 is divisible by k, which may be as large as 1e5, so q can be up to 10^9/1 = 10^9, which is fine.
Now, potential overflow in computing q = (n+1)/k; we must ensure that n+1 fits in integer type, but it's less than 2^31, okay.
Now, we also should think about using modinv correctly: In case of r_pow_k == 1, we skip inverse.
Now, ensure that we handle negative modulus properly: In else case, we compute numerator = (powmod(r_pow_k, q) - 1 + MOD) % MOD; denominator = (r_pow_k - 1 + MOD) % MOD; then inv_den = modinv(denominator); geom = numerator * inv_den % MOD.
Now, what if denominator becomes 0 after adding MOD? Actually if r_pow_k - 1 is 0 mod MOD, then after adding MOD we get MOD, and % MOD gives 0. But we are in else case only if r_pow_k != 1, so r_pow_k - 1 mod MOD != 0, so denominator after (r_pow_k - 1 + MOD) % MOD is non-zero and equals r_pow_k - 1 (since r_pow_k - 1 is between -MOD+1 and MOD-1). For r_pow_k not equal 1, r_pow_k - 1 is between 1 and MOD-2 (if positive) or negative. Adding MOD ensures it's positive. So denominator is non-zero, good.
Now, I'll produce final answer.
But hold on: There is a subtlety: The sum S = sum_{i=0}^{n} s_i a^{n-i} b^{i} could be computed differently, but our grouping yields sum_r = sum_{j=0}^{k-1} s_j r^j, and geom = sum_{t=0}^{q-1} (r^k)^t. However, what about the factor a^n? That's okay.
But consider if we compute total = geom * sum_r, but note that this product is sum over j of s_j r^j * geom. However, geom is sum_{t=0}^{q-1} (r^k)^t. So product yields sum_{j} s_j r^j * sum_{t} (r^k)^t = sum_{j,t} s_j r^{j + t k}. That's correct. So final S = a^n * sum_{j,t} s_j r^{j+tk} = sum_{j,t} s_j a^n * (b/a)^{j+tk} = sum_{j,t} s_j a^{n-j-tk} b^{j+tk}. But is that equal to the original? Because original index i = j + tk, and exponent of a is n - i, so indeed a^{n - (j+tk)} = a^{n - j - tk}. Multiply by b^{j+tk} gives a^{n} (b/a)^{j+tk}. So matches.
Thus correct.
Now, one more nuance: Our sum_r is sum_{j=0}^{k-1} s_j r^j. But what about the order? In the double sum, j runs over 0..k-1, t runs over 0..q-1. That's all i from 0 to n because n = kq - 1, so indices: i = j + tk, with j from 0..k-1, t from 0..q-1, gives exactly n+1 distinct indices. Yes.
Thus solution is correct.
Now, answer.
But wait: There is a potential catch: The problem statement says: "Find out the non-negative remainder of division of ∑ {i=0}^{n} s{i} a^{n - i} b^{i} by 10^{9} + 9." But the modulo is unusual! It's 10^9+9, which is prime. So we can compute modular inverses.
Now, just in case, we might want to avoid computing modular inverse of a if a is 1? But it's fine.
Now, final answer.
But I recall that in some Codeforces problems, the modulo 1e9+9 is used to avoid collisions. So it's okay.
Now, I'll produce the answer in final channel.
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000009LL;
// fast exponentiation modulo MOD
long long modpow(long long base, long long exp) {
long long result = 1;
while (exp > 0) {
if (exp & 1)
result = (result * base) % MOD;
base = (base * base) % MOD;
exp >>= 1;
}
return result;
}
// modular inverse using Fermat's little theorem
inline long long modinv(long long x) {
return modpow(x, MOD - 2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n, a, b, k;
cin >> n >> a >> b >> k;
string s;
cin >> s; // length k
long long q = (n + 1) / k; // number of full periods, guaranteed integer
a %= MOD;
b %= MOD;
long long inv_a = modinv(a);
long long r = (b * inv_a) % MOD; // r = b / a (mod MOD)
long long r_pow_k = modpow(r, k); // r^k
// geometric sum of r^k over t = 0 .. q-1
long long geom;
if (r_pow_k == 1) {
geom = q % MOD;
} else {
long long numerator = (modpow(r_pow_k, q) - 1 + MOD) % MOD;
long long denominator = (r_pow_k - 1 + MOD) % MOD;
geom = numerator * modinv(denominator) % MOD;
}
// sum over one period: Σ s_j * r^j
long long sum_r = 0;
long long cur = 1; // r^0
for (int i = 0; i < k; ++i) {
if (s[i] == '+')
sum_r = (sum_r + cur) % MOD;
else
sum_r = (sum_r + MOD - cur) % MOD; // -cur
cur = (cur * r) % MOD;
}
long long total = (geom * sum_r) % MOD;
long long a_pow_n = modpow(a, n);
long long ans = (a_pow_n * total) % MOD;
cout << ans << endl;
return 0;
}