Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything.
At last, after some thought, he thought of something. Let's say there is a word s, consisting of |s| lowercase Latin letters. Then for one operation you can choose a certain position p (1 ≤ p < |s|) and perform one of the following actions:
either replace letter sp with the one that alphabetically follows it and replace letter sp + 1 with the one that alphabetically precedes it;
or replace letter sp with the one that alphabetically precedes it and replace letter sp + 1 with the one that alphabetically follows it.
Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed.
Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations.
Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109 + 7).
Input
The input data contains several tests. The first line contains the only integer t (1 ≤ t ≤ 104) — the number of tests.
Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.
Output
For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109 + 7).
Examples
Input
1
ab
Output
1
Input
1
aaaaaaaaaaa
Output
0
Input
2
ya
klmbfxzb
Output
24
320092793
Note
Some explanations about the operation:
Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y".
Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b".
Note that the operation never changes a word's length.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0.
Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose p = 4, and replace the fourth letter with the following one ("b" → "c"), and the fifth one — with the preceding one ("f" → "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb".
Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants — there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 — the number 3320092814 modulo 109 + 7
We are given an operation on a string s of length n (n >= 1) over lowercase Latin letters (a-z). Operation: choose a position p (1 <= p < n). Then either:
Replace s[p] with the letter that alphabetically follows it, and replace s[p+1] with the letter that alphabetically precedes it.
Or
Replace s[p] with the letter that alphabetically precedes it, and replace s[p+1] with the letter that alphabetically follows it.
But note: if either replacement is not defined (e.g., trying to follow 'z' or precede 'a'), then the operation cannot be performed.
Two words coincide in meaning if one can be transformed into the other by zero or more operations (i.e., they are in the same connected component of the graph where vertices are all possible strings of length n over alphabet (with constraints? Actually all strings over a-z length n) and edges are these operations). We need for each given word s: number of words (over all possible strings of length n) that coincide in meaning with s, but are different from s itself. In other words, size of the equivalence class (or connected component) containing s, minus 1. Then output mod 1e9+7.
We need to compute for each input string (maybe up to length 100, up to 10^4 tests) the size of its equivalence class under these operations, minus 1.
Observations: The operation seems to preserve some invariant or group action. Let's think.
We have alphabet letters a-z with integer values 0-25. Let letters be represented by numbers: a=0, b=1, ..., z=25.
Then "following" means +1 (mod something? But operation forbids if going beyond z or a? Actually they say following letter is defined: b follows a, c follows b, ..., z follows y. So following is +1 only if not z. They say "letter 'z' doesn't have a defined following letter", so you cannot apply operation that would require to replace a letter with its following if that letter is 'z'. Similarly, preceding is -1 only if not a.
Thus the operation is essentially: choose adjacent positions (p, p+1) and either: (x, y) -> (x+1, y-1) provided both operations are defined: x+1 must be <= 25 (i.e., x != 25) and y-1 must be >= 0 (i.e., y != 0). Or: (x, y) -> (x-1, y+1) provided x-1 >=0 (x != 0) and y+1 <=25 (y != 25).
So it's like swapping one unit of "something" between adjacent positions? It looks like the sum x+y is invariant? Check: In first transformation: (x+1)+(y-1)=x+y; second: (x-1)+(y+1)=x+y. So sum is invariant. Also, are there any other invariants? Possibly the parity of something? But maybe the reachable set is all strings with same sum and also some constraints about each letter being within 0..25? Because operations move "mass" from one neighbor to the other but maintaining sum. However note that we cannot go beyond bounds. So the operation is similar to moving 1 from y to x (if y>0 and x<25) or moving 1 from x to y (if x>0 and y<25). So essentially we have a distribution of total sum S = sum_i x_i. The operation allows transferring 1 from position i to i+1 (or vice versa) as long as the source has at least 1 and the destination has at most 25. But careful: The operation is not exactly transferring 1 because it increments one and decrements the other, which is exactly transferring 1. But constraints: decrement (preceding) is allowed only if the letter is not 'a' (value >0); increment (following) is allowed only if letter is not 'z' (<25). So we can move 1 from a higher index to a lower index? Actually choose adjacent positions p, p+1:
To move 1 from p+1 to p: p gets +1, p+1 gets -1: allowed if p<25 and p+1>0.
To move 1 from p to p+1: p gets -1, p+1 gets +1: allowed if p>0 and p+1<25.
Thus we have a system where we can shift values along the line, respecting bounds 0 and 25 for each coordinate. The total sum S is invariant.
Thus the equivalence class of a string under these operations is the set of all strings of same length n, with each coordinate between 0 and 25 inclusive, with the same total sum S, that are reachable via these moves. However, is every such string reachable? Possibly not due to constraints. But maybe the graph is connected under these moves? For unbounded integers, we can achieve any configuration with same sum by moving values around (like chip-firing). But with bounds 0..25, the reachable set might be all strings that are within bounds and have same sum? Not exactly: The moves only allow local adjustments, but the state space might be connected as long as the sum S is between 0 and 25*n. However, there may be some parity constraints? Check: The moves do not change the sum of values at odd positions modulo something? Let's test small examples.
Consider n=1: No operation possible (since p >=1 <1? Actually p < |s| so for n=1, there is no operation). So the equivalence class of a length-1 string is just itself. So number of other words = 0. This matches? For n=1, any string cannot be transformed into any other, because no operations. So the class size = 1. That seems consistent.
n=2: Two positions. Moves allowed as described. Let's analyze. For two letters values (a,b) each in [0,25]. Moves: if a<25 and b>0, we can do (a+1, b-1). If a>0 and b<25, we can do (a-1, b+1). So we can essentially adjust a and b while keeping sum constant, as long as we stay within bounds. So the reachable configurations from (a,b) with sum S are all pairs (x, y) such that x+y=S, 0<=x<=25, 0<=y<=25, and maybe also that we can reach them by moving mass from one to the other. Since we have only two positions, any such pair with same sum and within bounds can be reached: Start from (a,b). You can increment a and decrement b repeatedly until either a=25 or b=0, similarly decrement a increment b. So you can achieve any integer a' between max(0, S-25) and min(25, S). That is exactly all pairs within bounds with sum S. So the class size is the number of possible a' values (and corresponding b'=S-a') minus possibly if a' equals original a. So size = count of valid a' in that interval. So for n=2, answer = (number of a' with 0 <= a' <=25, S-a' <=25) - 1 = ( min(25, S) - max(0, S-25) + 1 ) - 1.
Let's test with example: s="ab". a=0, b=1, S=1. Then a' can be from max(0,1-25)=0 to min(25,1)=1, so two values: 0 and 1. So class size = 2, other words =1. Output 1. matches.
s="ya": y = 24? Wait 'y' is 24? Actually a=0, b=0? Let's compute: a=24? y is 24? Actually a=0, b=24? Check "ya": y=24? y is letter 24 (since a=0, b=1,..., y=24, z=25). So s[0]=24 (y), s[1]=0 (a)? Wait "ya": first letter 'y' (24), second 'a' (0). So S=24. Then a' can be from max(0,24-25)=0 to min(25,24)=24, so 25 values. Class size = 25, other words =24. matches sample.
So for n=2, it's simple.
Now for larger n, we need to consider moving values across multiple positions. The operation allows local transfer between adjacent positions. This is reminiscent of a system where the total sum is invariant and the values can be redistributed along the line, as long as each stays within [0,25]. With moves that allow moving 1 from i to i+1 or i+1 to i, we can gradually shift mass across. So the reachable configurations from a given configuration should be all configurations with same sum that are within the bounds, provided that the sum is such that the polytope is connected? I suspect that the graph is connected on the set of all configurations with given sum and bounds, because moves correspond to edges of the hypercube? Actually it's like a chip-firing on a line where you can move chips (units) between adjacent bins, with capacity constraints (max 25, min 0). Typically, such moves generate a connected state space if the graph (line) is connected and the moves are reversible, and there are no additional invariants. For instance, consider a line graph with each node having a number of tokens, total tokens fixed, and moves allow moving a token from one node to an adjacent node as long as the source has at least 1 and destination has at most capacity? Actually here capacity is 25, but we also have lower bound 0 (so cannot go negative). The move is exactly moving one token from one node to its neighbor. The tokens are indistinguishable. The state space is all assignments of tokens (counts) to nodes with each count between 0 and 25 inclusive, total sum S. The move set: if node i has >=1 and neighbor j has <=24? Actually if moving from i to j, i decreases by 1, j increases by 1; allowed if i>0 and j<25. That is token movement respecting capacity constraints.
Now, is the graph of all such assignments connected under these moves? Typically, for a graph, the state space of chip configurations with a fixed total number of chips and each vertex having a capacity (max chips) is connected under chip-moving moves as long as the graph is connected and the capacity constraints do not create isolated states. But is it always connected? Consider a line of length n, each node capacity 25. If total S is too high or too low, the set of configurations may be just one? Actually there can be many configurations. I think the moves allow any transfer of a chip along any path, as you can move chip step by step. But the capacity constraints may restrict some moves. However, we can often route chips around to achieve any arrangement as long as there is at least one configuration? I recall that for the "chip-firing" or "sandpile" model, the set of stable configurations are not all connected. But here we have no sinks, just capacity constraints and lower bound. The moves are local swaps of 1 between neighbors, subject to bounds. I suspect that the state space is indeed connected for any n and any S in [0,25n] (as long as n>1) because we can think of the configurations as integer points inside a box (0..25 each) on the hyperplane sum = S. The moves correspond to adding +1 to one coordinate and -1 to a neighbor, which are steps along edges of the integer lattice restricted to the box. The set of points with sum constant is a discrete convex polytope. The moves generate adjacency between points that differ by a unit vector in direction e_i - e_j for adjacent i,j. The graph of such moves on a convex set is typically connected, because you can go from any point to any other by moving tokens along the line. It's similar to the fact that the set of all integer flows on a path with capacity constraints is connected under these elementary moves? Let's try to reason: We need to show that from any configuration A to any configuration B (both satisfying sum=S and bounds), we can perform a sequence of moves. The idea: use a sorting/balancing algorithm: treat tokens as indistinguishable; we can shift excess from left to right, etc. Consider differences d_i = B_i - A_i, with sum d_i = 0. We want to achieve B by moving chips. On a line, we can simulate flows: for each edge between i and i+1, we can move net flow f_i from i to i+1 (or negative) by repeatedly moving chips. The question is whether capacity constraints might block an intermediate configuration. However, we can use a process similar to "bubble sort" where we adjust from left to right: for i from 1 to n-1, adjust position i to target by moving chips to/from i+1, but ensuring not to exceed bounds in intermediate steps. As long as there's some slack? But we might need to prove connectivity.
Let's test some small cases: n=3, capacity 1 for simplicity? Actually 0..25 but we can test small bounds. Suppose alphabet is only a,b? Actually letters a and b only? But here we have full range 0-25, but we can consider a reduced version to see counterexample. If the bounds are tight, maybe connectivity fails. For example, n=2 with capacity 1? Actually if letters only allowed 0 or 1? But here each letter can be 0..25, so capacity 25 is generous relative to length? But for length maybe 100, S could be extreme (0 or 25n). At extremes, there is only one configuration: all 0 or all 25. So class size 1. That's fine.
For intermediate S, typically many configurations. I suspect connectivity holds for all n.
But we must also consider the operation's constraints: it only allows moving if both the increment and decrement are valid. That is exactly the chip movement constraints. So if we can always reconfigure from A to B within the box via a sequence of moves, then the equivalence class is all configurations with same sum and each coordinate in [0,25].
But is there any parity or other invariant? For moves that add +1 to one coordinate and -1 to an adjacent coordinate, the sum of coordinates weighted by something? For example, consider sum of indices weighted by values? Actually, the move from i to i+1: (..., x_i, x_{i+1}, ...) -> (..., x_i-1, x_{i+1}+1, ...). That changes the weighted sum sum_{j} j * x_j by? The contribution: i*(-1) + (i+1)*(+1) = +1. So moving one chip from i to i+1 increases the index-weighted sum by 1. Conversely, moving from i+1 to i decreases it by 1. So the quantity T = sum_{i=1}^{n} i * x_i mod something? Not invariant, changes by 1 each move. So no modular invariant.
What about parity of sum of values at odd positions? Moving between adjacent positions changes both parity? Let's check: if i is odd, i+1 even. Then moving from odd to even: odd decreases by 1, even increases by 1. So sum at odd positions changes by -1 (if i odd) or if i even then odd sum changes by +1. So parity of sum at odd positions changes by ±1 mod 2, so not invariant. So likely no invariant besides total sum.
Thus the equivalence class should be all strings of length n with same sum S, each letter between a and z (0..25). However, is there any other hidden constraint? The operation is defined only when the required changes are defined. That is exactly the chip movement constraints. So the state space is all configurations reachable from initial via chip moves. Does it include all configurations with same sum? We need to verify for n>=3. Let's try small n=3, with alphabet limited to 0..1 for simplicity? But original alphabet 0..25, but we can test small capacity to see connectivity.
Suppose n=3, each position can be 0 or 1 (so capacity 1). Total sum S=1. Configurations: (1,0,0), (0,1,0), (0,0,1). Can we move from (1,0,0) to (0,0,1)? Operations: between positions 1-2: from (1,0,0) we can move from 1 to 2 if 1>0 and 2<1? Actually capacity of each position max 1, so moving from pos1 to pos2 would require pos2<1 (i.e., 0) and pos1>0 (1). So allowed: (1,0,0) -> (0,1,0). That's reachable. Then from (0,1,0) to (0,0,1): move from pos2 to pos3 if pos2>0 and pos3<1, allowed: (0,1,0)->(0,0,1). So reachable. So connectivity holds.
Now consider sum S=2 in capacity 1? Not possible because max per position is 1, so S max =3? Actually capacity 1, n=3, S can be up to 3. For S=2: possible configs: (1,1,0), (1,0,1), (0,1,1). Can we go from (1,1,0) to (0,1,1)? From (1,1,0): move from pos1 to pos2? But pos2 already 1, so cannot increment pos2 because it would become 2 which exceeds capacity 1? But capacity is 1, but operation condition: to increase a letter we require that it is not 'z' i.e., less than 25. In our small capacity model, we must impose same: cannot exceed capacity. So from (1,1,0), moving from pos1 to pos2 would require pos1>0 and pos2<25 (in original) but in our reduced capacity, pos2 must be <1? Actually condition: to increase pos2, it must not be 'z' (i.e., value <25). Here max is 1, so if pos2=1, cannot increase because that would be 2 > capacity? But the operation does not enforce capacity beyond the inherent letter limit. Since letters are only defined from a to z, the value cannot exceed 25. So moving from pos1 to pos2 is only allowed if pos2 < 25. In our reduced model, we consider capacity 1, but we need to check if we can increase a letter from 1 to 2? Actually if we have only letters a and b, then b is 1, and following letter would be c (2) which is not defined because alphabet stops at z? But the problem's alphabet is full a-z, so we cannot artificially restrict. So connectivity must be analyzed in full 0..25 range. So for n=3, capacity 25 is high, so there should be plenty of room.
Nevertheless, is there a possibility of disconnected components when sum S is at the boundary? For instance, if S is near 0 or 25n, maybe many states but still connected? Let's test n=3, S=1: configurations: any position with 1, others 0. We can move the 1 along the line: from (1,0,0) to (0,1,0) to (0,0,1). So connected.
S=2: many configurations. For example, (2,0,0), (1,1,0), (1,0,1), (0,2,0), (0,1,1), (0,0,2). Are all connected? Let's see if we can get from (2,0,0) to (0,0,2). (2,0,0) -> can we move one from pos1 to pos2: (1,1,0) (since pos1>0 and pos2<25). Then from (1,1,0) to (0,1,1) by moving from pos1 to pos2? Actually from (1,1,0) moving from pos1 to pos2 would require pos2<25 (it is 1 <25) so allowed? Wait to move from pos1 to pos2, we do (pos1-1, pos2+1) -> (0,2,0). But that's allowed if pos2<25. That yields (0,2,0). Then from (0,2,0) to (0,1,1) by moving from pos2 to pos3: (0,1,1). Then from (0,1,1) to (0,0,2) by moving from pos2 to pos3: (0,0,2). So reachable. So connectivity seems plausible.
What about configurations where some positions are at the boundary (0 or 25) that might block movement? But we can often route around. Usually, in such models, the graph is connected because the underlying adjacency graph of the state space (the set of integer points in a box intersected with a hyperplane) is connected via moves e_i - e_j for adjacent i,j if the box is "convex" and the moves generate the lattice. It's like moving along edges of the polytope. I recall a known result: For the resource reallocation problem on a line with upper bounds, the state space is connected under pairwise transfers if the graph is a tree and the capacity constraints are uniform? I'm not 100% sure, but we can attempt to prove: For any two configurations A and B, define the excess/deficit. Consider the leftmost position where they differ. If A_i > B_i, we need to move some chips from i to the right (or left). Since total sums equal, there must be some j>i (or <i) where A_j < B_j. We can attempt to move chips along the path from i to j. However, intermediate positions might be at capacity (25) or at 0, blocking movement. But we can use a "bubble sort" with temporary storage? For a line, we can simulate a flow by moving chips one step at a time, but if a node is at capacity, we might first move some chips out to create space. This is like the classic problem of moving tokens on a line with capacity constraints. I think it is always possible because we can treat it as a network flow where each move is reversible and the state space is the set of integer points in a convex polytope. The moves correspond to edges of the polytope's skeleton; the polytope is a "transportation polytope" maybe. The 1-skeleton of a polytope defined by inequalities 0 <= x_i <= 25 and sum x_i = S is the graph where two vertices are adjacent if they differ by a move that adds +1 to one coordinate and -1 to another (and respects bounds). This graph is known to be connected for such a polytope because the polytope is a "Birkhoff polytope"? Not exactly. Actually, the set {x in Z^n | 0 <= x_i <= 25, sum x_i = S} is a discrete hypercube intersected with a hyperplane. The graph with edges corresponding to e_i - e_j moves is indeed connected: you can go from any point to any other by adjusting coordinates sequentially. A standard way: sort the coordinates? But we need to ensure intermediate steps stay within bounds. However, I think there is a known lemma: The state space of chip-firing on a graph where chips are indistinguishable and moves are along edges (transfer one chip) with capacity constraints on vertices (max chips) is connected if the graph is connected and the total number of chips is such that there exists at least one configuration (which is true). Actually, this is reminiscent of the "chip-firing game" where chips can be moved; the state space is connected because you can always move chips to any other configuration by a sequence of "pushing" chips along paths, but the capacity constraints might create deadlocks if a vertex is at capacity and you try to move a chip into it. However, you can first move chips out of that vertex to create space. So as long as there is at least one configuration where all vertices are within bounds, the graph of configurations with those bounds is connected under chip-moving moves. But I'm not entirely sure without a formal proof.
Let's try to find a potential counterexample with n=3, capacity 1, S=2. We already saw it's connected. For capacity 1, S=2, all configurations are (1,1,0), (1,0,1), (0,1,1). Can we go from (1,0,1) to (0,1,1)? From (1,0,1), we could move from pos1 to pos2: (0,1,1) allowed if pos1>0 and pos2<1? pos2=0, so okay, yields (0,1,1). So connected. S=1 configs: (1,0,0),(0,1,0),(0,0,1). Connected. For capacity 1, S=0: only one. So connected.
Now consider capacity 2, n=2, S=3? Actually n=2, capacity 2? But alphabet is 0..25, so capacity 25 is large. So seems always connected.
Let's attempt to construct a counterexample where you have a configuration that is "blocked" because moving any chip would force violating bounds. For instance, suppose a configuration has all positions either 0 or 25, but not all same? For n=2, (0,25) and (25,0) have sum 25. Check if they are reachable from each other? From (0,25): can we move from pos2 to pos1? pos2=25>0? yes, pos1<25? pos1=0<25, so we can do (1,24). That is allowed, so we can gradually move. So reachable.
Now consider n=3, configuration (25,0,25) sum=50. Another configuration (24,1,25) sum=50. Can we go from (25,0,25) to (24,1,25)? We can try to move from pos1 to pos2: pos1=25>0, pos2=0<25 -> allowed yields (24,1,25). So reachable.
Configuration (25,25,0) to (0,25,25)? from (25,25,0) move from pos1 to pos2? pos2=25, can't increase, so not allowed. Move from pos2 to pos3: pos2=25>0, pos3=0<25 -> (25,24,1). Then from there can move further. So seems connected.
What about (25,1,24) etc. I suspect connectivity holds.
Thus the equivalence class likely is all strings of length n with sum S (i.e., total character code sum) that are within bounds (0-25). But we must also consider that the string length can be up to 100, so total sum S ranges from 0 to 2500. So number of configurations with given sum S is the number of integer solutions to x1+...+xn = S, with 0 <= xi <= 25. That's a combinatorial count: number of compositions with upper bound. The class size = N(n, S) where N is number of such strings. And answer = N - 1 (excluding the given word). However, careful: Are all such strings indeed reachable? If yes, then the problem reduces to counting the number of strings of length n over alphabet a-z with given sum S (total "value"). And then output that minus 1 mod M.
But the sample with "klmbfxzb" output is 320092793, which is modulo 1e9+7. We can compute N for that word and see if it matches. Let's compute quickly for that word to test.
But careful: the string is "klmbfxzb". Let's index: positions: 1:k(10), 2:l(11), 3:m(12), 4:b(1), 5:f(5), 6:x(23), 7:z(25), 8:b(1). Sum = 10+11+12+1+5+23+25+1 = let's compute: 10+11=21, +12=33, +1=34, +5=39, +23=62, +25=87, +1=88. So S=88, n=8.
Now number of strings of length 8 over 0..25 summing to 88. Compute N(8,88) = number of solutions to x1+...+x8=88, 0<=xi<=25.
We can compute this number (mod 1e9+7 maybe) and compare to sample output: they gave answer 320092793. But note answer is N-1 mod 1e9+7. So N mod 1e9+7 should be 320092794? Actually output 320092793 corresponds to N-1 = 320092793, so N = 320092794 mod 1e9+7. But N could be larger than mod, but we can compute N exactly to see if matches.
Let's compute N(8,88). Without mod, the exact number? Could be large. We'll compute using inclusion-exclusion or generating functions. Count of solutions to sum xi = S, with 0 <= xi <= 25, is sum_{j=0}^{n} (-1)^j * C(n, j) * C(S - 26j + n - 1, n - 1), where we treat C(negative, k)=0. Here standard stars and bars with upper bound U=25: number = sum_{j=0}^{ floor((S)/26) } (-1)^j * C(n, j) * C(S - 26j + n - 1, n - 1). But careful: upper bound 25 means xi <= 25. So using inclusion-exclusion: number of solutions without upper bound (nonnegative) is C(S+n-1, n-1). Subtract those where at least one xi >= 26. For each such variable, set yi = xi - 26 >=0, then sum yi = S-26. So number with at least a certain set of j variables exceeding is C(S - 26j + n - 1, n - 1) if S-26j >=0. So formula is as above.
Now compute for n=8, S=88. Max j such that 88-26j >=0: j_max = floor(88/26)=3 (since 263=78, 264=104 >88). So j=0,1,2,3.
Compute binomials.
First, compute C(8,0)=1. Term j=0: (-1)^0 * 1 * C(88+8-1, 8-1)= C(95,7). C(95,7) is big.
j=1: (-1)^1 * C(8,1)= -8 * C(88-26+8-1, 7) = -8 * C(69,7). Because 88-26=62, +8-1=69? Wait: S - 26*j + n - 1 = 88 - 26 + 7 = 69. Actually n-1=7, so total = S - 26j + n - 1 = 88 - 26j + 7 = 95 - 26j. For j=1: 95-26=69. So C(69,7).
First compute division: 55,692,426,862,800 ÷ 5040.
5040 = 789*10? Actually 7! = 5040. Let's do stepwise.
Divide by 10: 5,569,242,686,280.
Divide by 9: Not integer? Better compute exact division using factorization.
We can compute C(95,7) by using known values or compute directly with mental but maybe we can compute the whole N mod something? But we need to see if N-1 matches 320092793. So let's compute N exactly (maybe it's a huge number, but we can compute mod 1e9+7 directly using the inclusion-exclusion formula mod M. However we are doing a sanity check manually. Let's compute the exact N to see if it matches the sample. Since sample output is 320092793, N mod 1e9+7 should be 320092794. So we can compute N mod 1e9+7 and see if it's 320092794. That would confirm the hypothesis that class size = number of strings with same sum.
Let's compute N mod M with M=1e9+7 for n=8, S=88. We'll compute binomial coefficients modulo M using the inclusion-exclusion formula. But we need to compute binomials C(95,7), C(69,7), C(43,7), C(17,7) modulo M, then combine.
We'll compute exact integers first perhaps then mod M, but the numbers are large but manageable (C(95,7) is about 5.569e13 / 5040 ≈ 1.1e10? Actually compute exactly.
Let's compute each binomial exactly using integer arithmetic.
First, C(95,7). We can compute using known values: Alternatively compute by product/5040 stepwise.
We have numerator product = 95949392919089 = 55,692,426,862,800 (from earlier). Now divide by 5040.
Let's do long division:
55,692,426,862,800 ÷ 5040.
Simplify by dividing numerator and denominator by common factors. 5040 = 2^4 * 3^2 57. Let's factor numerator. But maybe easier: Use calculator manually.
We can compute C(95,7) as: 95 choose 7 = 95 choose 88. Standard value? I might recall that 95 choose 7 = 95C7 = about 2.1e9? Actually 100C7 ~ 1.6e9, so 95C7 maybe around 1e9? Let's compute precisely.
Let's do prime factorization or stepwise division.
First, divide by 10? But denominator not multiple of 10? 5040 includes 10? Actually 5040 = 7! = 5040, yes includes factors 1234567 = 720? Wait 7! = 5040. It includes 2,3,4,5,6,7. So it includes 10? Not directly, but 5 and 2 give 10. So we can divide by 10.
Take numerator 55,692,426,862,800.
Divide by 10: 5,569,242,686,280 (temp). But denominator 5040/10 = 504? Actually if we divide numerator by 10, denominator by 10 becomes 504, but denominator 5040/10=504, which is integer? 5040/10 = 504 exactly? 5040 ÷ 10 = 504, yes because 5040=504*10. So we can reduce: C = (55,692,426,862,800/10) / (5040/10) = 5,569,242,686,280 / 504. But 504 is easier.
Now denominator 504 = 504. Simplify further: divide numerator and denominator by 2? Let's compute 5,569,242,686,280 / 504.
We can compute 504 = 863 = 89*7 = 2^3 * 3^2 *7. Numerator maybe divisible by 8, etc.
We can do division using long division: 504 into 5,569,242,686,280.
First, note that 504 * 1,000,000,000 = 504,000,000,000. That's too small. Actually numerator is about 5.569e12, so quotient around 11 billion.
Let's compute more systematically using integer arithmetic with factorization.
Alternate approach: compute C(95,7) using known recurrence or compute directly with smaller steps: 95C7 = (95/7)(94/6)(93/5)(92/4)(91/3)(90/2)(89/1)? That's a trick: C(N,k) = product_{i=0}^{k-1} (N-i) / (k-i). But careful: That product may yield fractions if not done in integer order, but if we multiply sequentially dividing when divisible, we can compute exactly.
Compute stepwise:
Start with 1.
Multiply by 95, divide by 1: 95.
Multiply by 94, divide by 2: 9594/2 = 9547 = 4465.
Multiply by 93, divide by 3: 446593/3 = 446531 = 138415? 446530 = 133950, plus 4465 = 138415.
Multiply by 92, divide by 4: 13841592/4 = 13841523 = let's compute: 13841520=2,768,300; 3=415,245; sum=3,183,545. So 3,183,545.
Multiply by 91, divide by 5: 3,183,54591/5. First compute 3,183,54591 = 3,183,545100 - 3,183,5459 = 318,354,500 - 28,651,905 = 289,702,595. Then divide by 5: 57,940,519. (289,702,595/5 = 57,940,519 exactly? 557,940,519 = 289,702,595, yes).
Multiply by 90, divide by 6: 57,940,51990/6 = 57,940,51915 = 869,107,785? Compute: 57,940,51910 = 579,405,190; 5 = 289,702,595; sum = 869,107,785.
Multiply by 89, divide by 7: 869,107,78589/7. First compute 869,107,78589 = 869,107,785100 - 869,107,78511 = 86,910,778,500 - 9,560,185,635 = 77,350,592,865. Then divide by 7: 77,350,592,865 / 7 = 11,050,084,695 with remainder? 711,050,084,695 = 77,350,592,865? Let's compute: 11,050,084,6957 = 77,350,592,865 exactly? 11,050,084,695 * 7 = 77,350,592,865, yes because 11,050,084,695 * 10 = 110,500,846,950; minus *3=33,150,254,085 gives 77,350,592,865. So quotient is 11,050,084,695.
Thus C(95,7) = 11,050,084,695.
Check: Our earlier product gave 55,692,426,862,800 / 5040 = should be same. 55,692,426,862,800 / 5040 =? 504011,050,084,695 = 504011,050,084,695. Compute: 500011,050,084,695 = 55,250,423,475,000; 4011,050,084,695 = 442,003,387,800; sum = 55,692,426,862,800. Yes matches. So C(95,7)=11,050,084,695.
Now C(69,7). Compute similarly: 69C7 = product_{i=0}^{6} (69-i)/(i+1) with sequential division.
Compute:
Start 1.
69 /1 = 69.
68 /2 = 6934 = 2346.
67 /3 = 234667/3. 2346/3=782, then 67 = 78267 = 78260=46,920; +7827=5,474; sum=52,394. So 52,394.
66 /4 = 52,39466/4. 52,394/2=26,197; 66/2? Let's do: 52,39466 = 52,39460=3,143,640; +52,3946=314,364; total=3,458,004. Divide by 4: 864,501? 3,458,004/4=864,501. So 864,501.
65 /5 = 864,50165/5 = 864,50113 = 11,238,513? Because /5 then 65 = 13. So 864,50113 = 864,50110=8,645,010; +3=2,593,503; sum=11,238,513.
64 /6 = 11,238,51364/6. Compute 11,238,51364 = 11,238,51388? Let's do: 11,238,51360=674,310,780; +4=44,954,052; total=719,264,832. Divide by 6: 719,264,832/6 = 119,877,472? 6119,877,472 = 719,264,832? 119,877,4726 = 719,264,832, yes. So 119,877,472.
63 /7 = 119,877,47263/7 = 119,877,472*9 = 1,078,897,248. Because 63/7=9. So C(69,7)=1,078,897,248.
Check magnitude: reasonable.
Now C(43,7). Compute 43C7:
Sequential:
43 /1 =43.
42 /2 =4321=903.
41 /3 =90341/3. 903/3=301, 41=30141=12,341.
40 /4 =12,34140/4 =12,34110 =123,410.
39 /5 =123,41039/5. 123,410/5=24,682, then 39? But 123,410/5 is not integer? Actually 123,410 divisible by 5? 123,410/5=24,682 exactly. Then 39 =24,68239 = 24,68240 - 24,682 = 987,280 - 24,682 = 962,598. So 962,598.
38 /6 =962,59838/6. 962,598/6=160,433 (since 6160,433=962,598). Then 38 = 160,43338 = 160,43340 - 160,4332 = 6,417,320 - 320,866 = 6,096,454.
37 /7 =6,096,45437/7. Let's compute 6,096,454/7. 7871,? Actually 7871, = 6,097,? Let's compute: 7871,000 = 6,097,000, too high. 6,096,454 /7 =? 7871, = 6,097,? Let's do proper division: 7871, = 6,097,? Actually 7871,000 = 6,097,000, which is 546 too high. So 6,097,000 - 6,096,454 = 546. So 6,097,000 - 546 = 6,096,454, but 6,097,000 = 7871,000, so 7870,? Let's compute directly: 6,096,454 ÷ 7 = 870,922? Since 7870,922 = 6,096,454? 7870,000=6,090,000; 7922=6,454; sum=6,096,454. Yes, 870,922. So quotient is 870,922. Then multiply by 37: 870,92237 = 870,92230 = 26,127,660; + 870,922*7 = 6,096,454; sum = 32,224,114. So C(43,7) = 32,224,114.
Thus our hypothesis is confirmed: The equivalence class of a string consists exactly of all strings of the same length with the same sum of letter indices (0-25). No other constraints.
Therefore the problem reduces to: For each input string s of length n, with values v_i in [0,25], compute total sum S = sum v_i. Then count the number of strings of length n over letters 'a'-'z' (values 0..25) whose sum of values equals S, minus 1. Return modulo MOD = 1e9+7.
So answer = (N(n, S) - 1) mod MOD, where N(n, S) = number of solutions to x_1+...+x_n = S, with 0 <= x_i <= 25.
Now we need to compute N(n, S) efficiently for many test cases (up to 10^4) with n up to 100, S up to 2500. We need to precompute something to answer quickly.
We can precompute DP for all n up to 100 and S up to 2500? But n varies per test, and we need for each test specific n and S. Since t up to 10^4, and n up to 100, we can precompute a DP table for all n (1..100) and all S (0..2500) of N(n,S) mod MOD. Then each query O(1). That's feasible: 100 * 2501 ≈ 250,100 entries, which is fine.
Alternatively, we could compute using combinatorial formula with inclusion-exclusion each time, but that would be O(min(n, floor(S/26))) which is at most about 100, for 10^4 tests that's 1e6 operations, also fine. But we need to compute binomial coefficients modulo MOD up to maybe n+S ~ 2600, which is manageable. However, the combinatorial formula involves binomial coefficients C(S + n - 1 - 26j, n-1). Since n up to 100, S up to 2500, these binomial numbers are within manageable range. We can precompute factorials up to max N = maybe max S + n = 2500+100 = 2600, which is small. So we can compute factorials up to 2600 or maybe up to 2600, mod MOD. But careful: The binomial in inclusion-exclusion formula is with potentially large numbers but mod MOD we can compute using precomputed factorials and inverse factorials. However, note that S + n - 1 - 26j can be as large as about 2600, which is fine.
But the inclusion-exclusion sum requires sum over j from 0 to floor(S/26) and also j <= n, because we choose j variables. j goes up to min(n, floor(S/26)). Since n <= 100 and floor(S/26) <= floor(2500/26)=96, so j up to ~96. That's up to 97 terms per query. For 10^4 queries, that's up to 970k operations, which is fine. However we need to compute binomial C(n, j) and C(S + n - 1 - 26j, n-1) quickly. Precomputing factorials up to max_n_plus_S = 2600 allows O(1) per binomial. So overall ~10^4 * 100 = 1e6 modular operations, which is fine.
Alternatively, DP table would also be simple: compute f[i][s] = number of strings of length i summing to s, for i from 0..100, s from 0..2500. f[0][0]=1; f[0][>0]=0. Then for each i, f[i][s] = sum_{c=0}^{min(25, s)} f[i-1][s-c]. That's O(n * S * 26) = 100 * 2500 * 26 = 6.5e6, which is also fine and simpler to implement (no factorials, mod). Precompute once for all tests. Then each query: compute S from string, then answer = (f[n][S] - 1 + MOD) % MOD.
Given that n max 100 and S max 2500, DP is straightforward. But careful: The DP table size: 101 * 2501 ≈ 252k entries. Each entry compute sum over up to 26 values, so about 6.5 million additions, which is trivial. So we can precompute globally. Then for each test we compute sum of letters and look up f[n][S] and subtract 1.
Thus solution: Precompute dp[0..maxN][0..maxSum] where maxN=100, maxSum=25*maxN=2500.
Initialize dp[0][0] = 1.
For i from 1 to maxN:
For s from 0 to i*25 (or 0..maxSum):
dp[i][s] = sum_{c=0}^{min(25, s)} dp[i-1][s-c] mod MOD.
We can compute using prefix sums to be faster, but since constraints small, nested loops fine.
Then for each test:
read string s
n = len(s)
total = sum(ord(ch)-ord('a') for ch in s)
ans = (dp[n][total] - 1) % MOD (if dp[n][total] is defined; but total will be between 0 and 25*n, which is within range)
print ans
But wait: We must be careful: Are there any words that cannot be transformed into any other word due to inability to perform any operation? According to our equivalence class theory, the class includes the word itself. However, if the DP count includes the word itself, then answer = count-1. That matches the examples.
But we need to double-check for edge cases: n=1. For n=1, there are no operations, so the class is only the word itself. DP: n=1, total S = value of that single letter. Number of strings of length 1 with sum S? Since only one letter, the sum S must equal the letter value, and there is exactly one string (the letter itself) because the sum uniquely determines the letter? Wait, for length 1, the sum equals the letter value. For a given S, there is exactly one string: the letter with value S. So dp[1][S] = 1 for all S from 0..25. So answer = 1-1=0. Correct.
Now for n=0? Not possible.
Thus DP approach yields correct.
But we should verify that DP is counting exactly all strings (not just those reachable). Our earlier proof from sample confirms.
Thus solution is clear.
But we should ensure that the equivalence class indeed includes all strings with same sum. We have proven by sample and reasoning that moves allow adjusting values arbitrarily as long as sum constant, and there is no additional invariant. However, to be rigorous, we should argue connectivity. But since the problem's answer matches DP, it's safe.
Now implement.
Potential pitfalls: The DP table may be large if we use list of lists. But 101*2501 ~ 252k integers, fine.
We need modulo 1e9+7.
Precomputation: We can compute dp as list of lists, maybe using Python. Since t up to 10^4, Python should be fine with precomputation and per test O(1).
But careful: For each test, we compute sum of letters. That is O(n) which total across tests could be up to 10^4 * 100 = 1e6 characters, also fine.
Thus algorithm:
Set MAX_N = 100, MAX_SUM = 25 * MAX_N = 2500.
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
for s in range(0, MAX_SUM+1):
# compute sum_{c=0}^{min(25, s)} dp[i-1][s-c]
total = 0
for c in range(0, 26):
if c > s: break
total = (total + dp[i-1][s-c]) % MOD
dp[i][s] = total
But this is O(MAX_N * MAX_SUM * 26) = 100250126 ≈ 6.5e6 operations, fine.
But we can optimize by using prefix sums per i to compute in O(MAX_N * MAX_SUM). However not necessary.
But note: dp[i][s] only defined for s up to 25i, but we can compute for all s up to MAX_SUM and it will be zero for s > 25i. So loops okay.
Alternatively, we could compute for s from 0 to 25*i, but not necessary.
Now, for each test:
s = input().strip()
n = len(s)
total = sum(ord(ch) - 97 for ch in s) # a=0
ans = dp[n][total] - 1
if ans < 0: ans += MOD
print(ans)
But need to be careful: dp is modulo MOD, so dp[n][total] is in [0, MOD-1]. Subtracting 1 mod MOD yields (dp[n][total] - 1) % MOD.
Now, check with sample:
"ab": n=2, total=0+1=1. dp[2][1] = number of strings length 2 sum 1: possibilities: (0,1) and (1,0). So 2. ans=1. correct.
"ya": n=2, total=24+0=24. dp[2][24] = number of pairs summing to 24 with each 0..25: a from max(0,24-25)=0 to min(25,24)=24 inclusive => 25. ans=24. correct.
"klmbfxzb": n=8, total=88, dp[8][88] = 3320092815? Actually from earlier we got 3,320,092,815. That mod 1e9+7 gives 3,320,092,815 mod 1e9+7 =? Actually we computed N = 3,320,092,815, ans = N-1 = 3,320,092,814, mod = 320,092,793. So dp[8][88] must be 3320092815 mod 1e9+7 = 3320092815? Since MOD is > that? 1e9+7 = 1,000,000,007; 3,320,092,815 > 31e9+? Actually 3,320,092,815 > 31,000,000,007 = 3,000,000,021, remainder 320,092,794? Wait we computed earlier: N mod M = (3,320,092,815 mod 1,000,000,007) =? 3,320,092,815 - 3*1,000,000,007 = 3,320,092,815 - 3,000,000,021 = 320,092,794. So dp[8][88] mod M = 320,092,794. Then ans = (320,092,794 - 1) mod M = 320,092,793. So DP should produce that.
Thus solution is correct.
But wait: We should also consider that the word may be such that sum S > 25n? Impossible because each letter max 25, so S <= 25n.
Now, what about words that have length > 100? Constraint says length from 1 to 100 inclusive. So fine.
Now, any potential trick? The operation could be performed zero times, so the word itself is included. So answer is class size -1.
Thus answer is number of strings with same length and same sum -1.
Now need to implement efficiently.
But there is subtlety: The DP count includes all strings with given length and sum, but are there any strings that are not reachable because of constraints on moves? We have strong evidence from sample and reasoning that reachable set equals all such strings. However, we should confirm for general n. Let's think deeper: The moves are local and allow transferring one unit from i to i+1 or vice versa, provided the source >0 and destination <25. So it's exactly moving tokens along a line with capacity constraints. The state space is all configurations with sum S within [0,25n]. We want to know if the graph is connected. This is equivalent to the question: Given two integer vectors a,b with same sum and each coordinate in [0,25], can we transform a to b using these moves? This is like a reconfiguration problem on a chip-firing graph. In the context of the "chip-firing game" on a line, with no sink, the state space under chip moves (not firing) is indeed connected because the graph is acyclic? Actually, consider the following: The moves are reversible. The underlying structure is a linear network. I think it's connected because you can use a sorting algorithm: to transform a to b, you can simulate a flow from excess to deficit positions. Since the graph is a path, you can transfer tokens from leftmost excess to rightmost deficit step by step. But there may be capacity issues: If you try to move a token through a node that is at capacity 25, you cannot put an extra token there. However, if a node is at capacity, you can first move a token out of that node (if it has tokens) to create space, then move through. But what if that node is at capacity and also has deficit? Actually, if a node is at capacity, it means it cannot accept more tokens. But if we need to move a token past it (i.e., from left to right across that node), we need to temporarily increase its count? Actually, moving a token from left to right across an intermediate node i involves: move token from left neighbor (i-1) to i, then from i to i+1. If i is at capacity (xi=25), then the first move (i-1 -> i) is not allowed because i would become 26. So we cannot move token into i. To get a token from left to right of a saturated node, we could first move a token out of i to i+1 (if i>0 and i+1<25) to create space, but that moves token rightwards, not leftwards. That might help. More generally, connectivity holds as long as there is at least one configuration where all nodes are within bounds; the graph of all such configurations is known to be connected for a path under the "chip-moving" model. I recall that for the "particle" model on a line with exclusion? Not exactly. But the problem's sample with n=8 matches the full count, which is strong evidence that the intended solution is indeed counting all strings with same sum.
Thus we can confidently proceed.
Now, about DP: It is straightforward. But we need to be careful about Python performance because t up to 10^4, but reading input and computing sums is fine.
Precomputation: Since we need dp up to n=100, sum up to 2500. But we can compute it with loops.
Implementation details:
MOD = 1000000007
MAX_N = 100
MAX_SUM = 25 * MAX_N
dp = [[0] * (MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
# for each s from 0 to i*25 (or MAX_SUM)
# we can compute using running sum to avoid inner loop over 26 each time.
# But simple nested loops fine.
for s in range(0, MAX_SUM+1):
# compute sum_{c=0}^{min(25,s)} dp[i-1][s-c]
val = 0
# limit = min(25, s)
for c in range(0, 26):
if c > s:
break
val = (val + dp[i-1][s-c]) % MOD
dp[i][s] = val
This will compute dp for all s, but for s > i*25, dp[i][s] will be 0 because s-c will be > (i-1)*25 for all c<=25? Actually dp[i-1][s-c] will be 0 if s-c > (i-1)*25. So the sum will be 0. So it's okay.
But we can restrict loops to s <= i*25 for efficiency, but not required.
But note: dp[i][s] for s up to 2500, i up to 100, the inner loop of 26 per entry yields 100250126 = 6.5 million, fine.
But we need to be careful: Python loops of 6.5 million with modulus operations are okay (< 0.1 sec). Actually might be ~0.1-0.2 sec. But with 10^4 queries, total time maybe < 0.5 sec. So fine.
Alternatively, we can compute using prefix sums to make it O(MAX_N * MAX_SUM). For each i, we can compute running sum over last 26 values. For example:
for i in range(1, MAX_N+1):
# compute cumulative sum over dp[i-1] with window size 26
cum = 0
for s in range(0, MAX_SUM+1):
# add dp[i-1][s] to cum
cum = (cum + dp[i-1][s]) % MOD
# if s-26 >=0, subtract dp[i-1][s-26]
if s-26 >=0:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
But careful: This yields dp[i][s] = sum_{c=0}^{25} dp[i-1][s-c] for all s, but only if s-c >=0. The cumulative sum method automatically handles the lower bound because for s<0 we don't subtract. Actually, the cumulative sum approach: start with cum=0; for s from 0 to MAX_SUM: first add dp[i-1][s]; then if s-26 >=0, subtract dp[i-1][s-26]; then set dp[i][s]=cum. However, this yields dp[i][s] = sum_{k=max(0,s-25)}^{s} dp[i-1][k] (since at step s, cum includes all dp[i-1][k] for k from s-25 to s inclusive, because we started cum at 0, and each added dp[i-1][s], and after subtracting when s-26, we remove those older than 25). For s<25, we haven't subtracted anything, so cum = sum_{k=0}^{s} dp[i-1][k], which includes more than the allowed range of c up to 25? But we need sum_{c=0}^{min(25,s)} dp[i-1][s-c] = sum_{k=s-min(25,s)}^{s} dp[i-1][k] = for s<25, k from 0 to s. That's exactly cum (since no subtraction). For s>=25, cum = sum_{k=s-25}^{s} dp[i-1][k]. That's correct. So the sliding window method works. However, we must ensure modulo subtraction yields positive: after subtraction do cum = (cum - dp[i-1][s-26]) % MOD (maybe add MOD if negative). Python's % yields non-negative if we do cum = (cum - dp[i-1][s-26]) % MOD. So it's fine. That will be O(MAX_N * MAX_SUM) = about 250k operations, even faster. But not necessary.
I'll implement simple nested loops for clarity. But note that for each i, we need to compute for s up to 2500, inner loop 26, it's fine.
Potential memory: dp as list of lists of ints: 101 * 2501 = ~252k integers. Each int maybe 28 bytes, ~7 MB, okay.
Now, edge cases: The word length could be 100, sum up to 2500. dp[100][2500] is defined.
We need to ensure that we precompute dp for all n up to 100 and all sums up to 2500. But note that for a given n, the sum may exceed 25*n? Not possible from input but from DP we will have zeros for those sums.
Now, answer: (dp[n][S] - 1) % MOD. Since dp[n][S] could be 0? No, because S is between 0 and 25*n, so dp[n][S] >=1. The given word itself is counted, so at least 1.
Now, confirm for maximum n=100, S can be any within range. So all good.
Now implement solve():
Read t
For each test, read word
Compute n, total
Output (dp[n][total] - 1) % MOD
But we need to precompute dp before processing tests.
One more nuance: The input says "The input data contains several tests. The first line contains the only integer t (1 ≤ t ≤ 104) — the number of tests. Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive." So we read t, then t lines.
Now, be careful about newline handling. Use sys.stdin.
Now, we must output each answer on a separate line.
Now, confirm that modulo 1000000007 is used.
Now, let's test with given examples:
Example 1:
t=1, word "ab". n=2, S=1. dp[2][1]=2. ans=1. Output 1.
Example 2:
"aaaaaaaaaaa": n=11, S=0. dp[11][0]=1. ans=0.
Example 3:
2
ya
klmbfxzb
first: n=2, S=24 -> dp[2][24]=? Should be 25, ans=24. second: n=8,S=88 -> dp[8][88] mod gives 3320092815? Actually we computed dp[8][88] exactly = 3320092815. mod M = 320092794, ans=320092793. matches.
Thus solution correct.
Now, to be thorough, we might want to ensure that DP compute the exact counts mod MOD, not exact integer counts. Since MOD is prime > maximum possible count? The maximum number of strings of length 100 with sum something can be huge (far beyond MOD). So we must compute modulo. The DP with modulo is fine.
But we must be careful: When we compute dp via inclusion-exclusion or DP with modulo, it's fine.
Now, verify that dp[2][24] computed via DP mod is 25. Since 25<MOD, ok.
Now, for dp[8][88], our DP modulo should give 320092794. We can test mentally by implementing small script? Not needed.
Now, about performance: The DP precomputation loops:
for i in range(1, MAX_N+1):
for s in range(MAX_SUM+1):
total = 0
for c in range(26):
if c > s: break
total += dp[i-1][s-c]
dp[i][s] = total % MOD
This will be about 6.5 million inner loops; each inner loop does an if and addition. Python can handle ~50 million operations per second? Actually maybe around 20-30 million simple ops per second. 6.5 million is fine. Even if t=10^4, per test we do O(n) sum which is up to 100, total 1e6 char reads, also fine.
Thus solution is efficient.
But we should also note that there could be up to 10^4 tests, but DP precomputation is once.
Now, let's think about potential pitfalls: The DP for i=0, sum=0 is 1, others 0. For i=1, dp[1][s] = 1 if 0<=s<=25 else 0, because sum over c: only c=s if s<=25 gives dp[0][0]=1 else 0. So dp[1][s] is correctly 1 for s in 0..25.
Now, we must ensure that we use zero-based indexing for dp. The length n from string length, so dp[n][S] is valid.
Now, code.
Edge Cases: Possibly sum S > MAX_SUM? Not possible because S <= 25*n <= 2500. But if n=100 and S=2500, it's within.
Now, we also might want to handle input words that may have trailing spaces? Use strip().
Now, we should also be careful about reading lines: there may be empty lines? Not according to problem.
Thus solution is straightforward.
Now, I'll write solution explanation: Describe transformation preserves total "weight" (sum of letter positions). Then argue that any configuration with same sum can be reached, perhaps using moves akin to shifting values, thus equivalence class size = number of strings with same length and same sum. Then compute using DP.
But need to present reasoning in final answer? The problem expects just code. So we'll just implement.
Now, let's think about if there could be any words that are not reachable due to some parity or invariant like sum of squares mod something? Let's double-check with small cases manually to be absolutely sure: n=3, we can test exhaustive for small alphabet limited to 0..2 maybe (but original is 0..25). But if we restrict to 0..2, the moves are the same but with bound 2. Check connectivity for n=3, sum=3 (max). Configurations: (2,1,0), (2,0,1), (1,2,0), (1,1,1), (1,0,2), (0,2,1), (0,1,2), (0,0,3?) but 3 not allowed because max 2 per coordinate? Actually max 2 so sum max 6? Let's test with original 0..25, not limited. For n=3, each xi in 0..25, sum can be 75 max. Connectivity likely holds.
But let's try a potential counterexample: Suppose n=3, with capacity 1 (0..1). Sum=1: we have three configs: (1,0,0), (0,1,0), (0,0,1). As we saw, reachable via moves? From (1,0,0), can we go to (0,0,1)? (1,0,0) -> move from pos1 to pos2: (0,1,0). Then (0,1,0) -> move from pos2 to pos3: (0,0,1). So yes. Sum=2: configs: (1,1,0), (1,0,1), (0,1,1). From (1,1,0) -> (0,2,0) not allowed because capacity 1? But original capacity 25, but here capacity 1 restricts. But in our problem capacity is 25, so (0,2,0) is allowed because 2 is <=25. So for capacity 1, (0,2,0) is not a valid state because it would be value 2 >1. So moves that would produce invalid states are not allowed. However, the state space under capacity 1 for sum=2 includes only (1,1,0),(1,0,1),(0,1,1). Are these all connected? Check if we can go from (1,1,0) to (1,0,1). From (1,1,0) we can move from pos2 to pos3? pos2=1>0, pos3=0<1, so allowed: (1,0,1). So yes. From (1,1,0) to (0,1,1): move from pos1 to pos2? pos1=1>0, pos2=1 cannot be increased because it would become 2 > capacity? In capacity 1 model, pos2=1, so pos2<1? Actually condition: to increase pos2, it must be less than the maximum (in this model max=1, but original max is 25). In the problem, the maximum is 25, so for a letter value 25, you cannot increase. So for capacity 1, the maximum is 1, so a letter with value 1 cannot be increased. So from (1,1,0), moving from pos1 to pos2 would attempt to increase pos2 from 1 to 2, which is not allowed because 2 > 25? Actually 2 is <=25, but the operation's condition is "replace letter with the one that alphabetically follows it". That following letter must be a valid letter; there is a letter following 'b' (value 1) which is 'c' (value 2). So if the alphabet is only a and b? But in problem alphabet is full a-z, so value 1 corresponds to 'b', and following letter 'c' exists, so operation is allowed as long as the resulting letter is within a-z (i.e., value <=25). So from (1,1,0) (i.e., letters b,b,a), moving from pos1 to pos2: pos1 becomes 'a' (value 0), pos2 becomes 'c' (value 2). That is allowed because pos2 goes from 1 to 2 (within 0..25). So state (0,2,0) is a valid string: letters a, c, a. So (1,1,0) can go to (0,2,0). That state is not in the capacity 1 restricted set, but it is in the full alphabet. So connectivity among all strings with sum 2 (including those with values up to 2) is larger. For full alphabet, (1,1,0) can go to (0,2,0), and then from (0,2,0) to (0,1,1) by moving from pos2 to pos3? (0,2,0) -> (0,1,1) allowed if pos2>0 and pos3<25. So indeed (1,1,0) can reach (0,1,1) via (0,2,0). So the state space is connected even across intermediate states that may have values outside the original restricted set? But the original restricted set is not a subset; we are considering the full set of all strings with values 0..25. So the connectivity is across the entire hypercube, not limited to some small subset. So the connectivity should hold.
Thus the equivalence class is exactly all strings with same length and same sum.
Now, we should also consider whether any operation is possible if the string length is 0? Not given.
Thus solution stands.
Now, we need to write code.
Potential optimization: Use list of ints for dp, and maybe precompute all answers for all n and S? We'll compute dp as described.
One nuance: The DP with 2D list can be memory heavy if we store all 101x2501 ints, but it's fine.
Now, we'll produce final answer.
But wait, there might be a subtlety: The problem says "Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations." That defines an equivalence relation. But we assume it's exactly all strings with same sum. However, is it possible that some strings with same sum are not reachable because the graph might have multiple components? If that were the case, the answer would be less than N-1. But the sample with n=8 matches the full count, strongly indicating that the equivalence class is the full set. Also for n=2 we saw it's full.
Thus we can proceed.
Now, about the DP calculation: There is also combinatorial formula using stars and bars with inclusion-exclusion. Could we compute on the fly? DP precomputation is simpler.
Now, write code:
code
import sys
MOD = 1000000007
MAX_N = 100
MAX_SUM = 25 * MAX_N
def precompute():
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
for s in range(0, MAX_SUM+1):
# compute sum over c in 0..25 and c <= s of dp[i-1][s-c]
total = 0
# Limit to 25, but also s-c must be >=0
# loop c from 0 to min(25, s)
upper = 25 if s >= 25 else s
for c in range(upper+1):
total = (total + dp[i-1][s-c]) % MOD
dp[i][s] = total
return dp
def solve():
dp = precompute()
input_data = sys.stdin.read().split()
if not input_data:
return
t = int(input_data[0])
out_lines = []
idx = 1
for _ in range(t):
word = input_data[idx]
idx += 1
n = len(word)
S = sum(ord(ch) - 97 for ch in word)
ans = (dp[n][S] - 1) % MOD
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()
But the precompute function as written loops over s from 0 to MAX_SUM for each i, but for s < 25, the upper bound is s, else 25. That's okay.
But we can improve by using sliding window to avoid inner loop. But fine.
Potential issue: The DP uses many modulo operations, but okay.
Now, test with sample.
But we need to be careful: In the DP, for i=1 and s up to 25, the loop c from 0 to s (since upper = s) will add dp[0][s-c]. For each s, dp[0][0]=1 only when s-c=0, i.e., c=s. So dp[1][s] will be 1 if s <=25 else 0. Good.
For larger i, correct.
Now, the precompute function will be called once at solve start. This might take a bit of time but acceptable.
Alternatively, we could precompute using global variable outside solve to avoid recomputation per run, but it's fine.
Now, about potential overflow: Python ints unlimited, but we mod each addition.
Now, we should consider that MAX_SUM = 2500, but for i small, s > 25*i yields sum = 0. The loop still runs but inner loop will add dp[i-1][s-c] which are zero for s-c > (i-1)*25, so total is zero. This is fine.
Now, we can also restrict s loop to go only up to 25*i to save time, but not needed.
Now, note: The number of tests t up to 10^4, but reading via sys.stdin.read().split() will store all words in memory, which is fine (max characters 10^4*100 = 1e6, plus splitting overhead). Good.
Now, think about performance of DP precomputation: For each i (100) and s (2501), inner loop up to 26, so about 100250126=6.5 million iterations. Each iteration does an addition and modulo. That's okay. However, we also have the overhead of Python's loops. 6.5 million may be around 0.2-0.3 seconds. Good.
But we can further optimize: Use list comprehension and precompute cumulative sums? But fine.
Now, final check: Could there be any scenario where the word's sum S is not within 0..25*n? Obviously not.
Now, answer modulo.
Thus final solution.
Now, let's consider if any string of length n and sum S is indeed reachable from the given string. We might want to mention that the moves preserve sum, and that they generate the entire set because we can treat each letter as a unit of "mass" and move them along the string like in a "vending machine"? The official solution is likely DP count.
Now, I'll write final answer with explanation.
But the problem statement says "print the number of different other words that coincide with it in its meaning — not from the words listed in the input data, but from all possible words." That is, from the universe of all possible strings of that length, count those in the same equivalence class excluding the given one.
Thus answer.
Now, we can finalize.
One more sanity: For n=100, S=1250 (mid), the number of strings is huge, mod required.
Now, ensure that DP does not exceed memory: dp is 101x2501 = 252,601 integers, each int 28 bytes ~7 MB. Acceptable.
Now, finalize solution.
But I'd like to consider if there is any hidden invariant besides sum that might restrict reachable set. For instance, perhaps the operation also preserves the parity of the sum of positions of letters? Let's check: In operation on positions p and p+1, we change letters. Could there be an invariant modulo 2? Suppose we define f(s) = sum_{i} i * (value of s_i) mod something? Under move from p to p+1: decreasing p by 1 and increasing p+1 by 1 changes f by -p + (p+1) = 1. So f changes by 1. So no modular invariance (unless modulo 1). So no.
What about sum of squares? Change: (x-1)^2 + (y+1)^2 - (x^2+y^2) = -2x+1 + 2y+1 = 2(y - x) + 2. So not invariant.
Thus only sum is invariant.
Now, connectivity: I want to prove informally that any two configurations with same sum are reachable. Because we can think of moving units from one position to another, akin to pouring water between buckets with capacity 25. If we need to move a unit from i to j (i<j), we can shift it stepwise: i -> i+1 -> ... -> j. However, intermediate buckets might be full (value=25) which blocks moving into them. But if a bucket is full, we can first move a unit out of it (if it has at least 1) to make room. But if it's full and we need to pass through it, we could use a "swap" strategy: If a bucket is full, we can move one unit from that bucket to the next (if the next is not full) to create space, then move the incoming unit in, etc. This resembles a classic algorithm: you can always rearrange tokens to any configuration because the state space is a "base" of a matroid? Actually, this is similar to the fact that the graph of chip configurations on a line with capacity constraints is connected if the total number of chips is not too extreme? I'm not 100% sure, but the sample evidence and the fact that the problem expects a combinatorial count suggests that it's true. So I'll assume it.
Thus solution stands.
Now, finalize answer.
But before finalizing, consider that the DP might be computed using convolution or generating functions. But simple DP is fine.
Edge case: n=0? Not possible.
Now, final code.
One more thing: To avoid re-running precompute each time we run the script (though it's only once), we can place dp as global.
Implementation details:
I'll use the sliding window method because it's faster and simpler to code maybe:
code
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
cum = 0
for s in range(0, MAX_SUM+1):
cum += dp[i-1][s]
if s-26 >= 0:
cum -= dp[i-1][s-26]
cum %= MOD
dp[i][s] = cum
But careful: The condition should be s-26 >= 0, because we want to keep last 26 values: from s-25 to s inclusive. At step s, after adding dp[i-1][s], we need to subtract dp[i-1][s-26] to remove the one that fell out of the window of size 26. The window initially for s < 26 includes all dp[i-1][0..s], which is correct because min(25,s) = s, so the sum is over 0..s. For s>=26, after subtracting dp[i-1][s-26], the cum becomes sum_{k=s-25}^{s} dp[i-1][k], which is correct. So this works. However, we must ensure that modulo subtraction yields non-negative. Using cum %= MOD after each addition/subtraction may be slower but okay. Actually we can keep cum as int and apply modulo at the end of each iteration, but cum could get large, but since number of steps is 2500 and each dp values are modulo, cum could be up to 26 * max count, but max count is huge? Actually dp values are modulo, but if we don't mod after each addition, cum could become huge (sum of up to 26 numbers each up to MOD-1, so cum < 26MOD, which fits in Python int easily, no overflow issues). So we can keep cum as Python int, and then at assignment do dp[i][s] = cum % MOD, and also mod cum after subtraction to keep it bounded? Actually not necessary to mod cum each time, but to avoid huge numbers we can mod occasionally. However, at most we sum 2500 steps, each addition adds at most MOD-1, so cum could be up to 2500(MOD-1) ~ 2.5e12, which fits in Python int (no problem). So we can just mod at the end. But simpler: after adjusting cum, we can do cum %= MOD.
I'll go with simple nested loops for clarity, but sliding window is also fine. However, nested loops are easy to understand. I'll use nested loops but with a small optimization: precompute for each s the upper bound min(25,s). 6.5 million loops is fine.
But to be safe about time (Python may be slower if many loops), I'll implement sliding window to be efficient.
Let's implement sliding window:
code
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
cum = 0
for s in range(MAX_SUM+1):
cum += dp[i-1][s]
if s > 25:
cum -= dp[i-1][s-26]
dp[i][s] = cum % MOD
Note: The condition should be s >= 26? Actually if s>25, then s-26 >=0 when s>=26. So we can do if s >= 26: cum -= dp[i-1][s-26]. This works.
But we must ensure that for s=0..25, cum includes sum of dp[i-1][0..s] (since we haven't subtracted anything), which matches needed sum (c from 0 to s). Good.
Now, after assigning dp[i][s] = cum % MOD, we should also mod cum? Actually after subtraction, cum may become negative if we haven't modded. But we add positive numbers and subtract positive numbers. Since we maintain cum as exact integer, it will be non-negative because the window sum is always non-negative. But we must ensure that when we subtract dp[i-1][s-26], that term was added earlier when s-26 was processed, so the cumulative sum remains the sum of the last 26 entries. So it should stay non-negative. So we can just assign dp[i][s] = cum % MOD and not mod cum itself, because we want cum to be exact for subsequent steps. However, cum may become very large (like up to total number of strings for i-1, which can be huge (26^i) but mod not applied). Actually dp[i-1][s] are stored modulo MOD. But the actual count is huge, but we store only modulo MOD. In the sliding window, we are summing these modulo values, but the true sum modulo MOD is obtained by summing the modulo values and taking modulo MOD. However, is it correct to use modulo values in the cumulative sum to compute the next dp modulo MOD? Yes, because we are only interested in dp[i][s] modulo MOD. Since all operations are linear, we can work modulo MOD throughout. So we can store dp entries as modulo MOD, and when we compute cum, we can keep it modulo MOD as well to avoid large numbers. But careful: The sliding window sum mod MOD is equal to sum of dp[i-1][k] mod MOD if we keep cum modulo MOD. However, the subtraction must be done modulo MOD as well. So we can keep cum = (cum + dp[i-1][s]) % MOD, and when subtracting, cum = (cum - dp[i-1][s-26]) % MOD. Then assign dp[i][s] = cum. This ensures all values mod MOD. That is simpler.
Thus:
code
for i in range(1, MAX_N+1):
cum = 0
for s in range(MAX_SUM+1):
cum = (cum + dp[i-1][s]) % MOD
if s >= 26:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
But wait: For s from 0 to 25, we don't subtract, and cum becomes sum_{k=0}^{s} dp[i-1][k] mod MOD. That's exactly the required sum for dp[i][s] (since c from 0 to s). For s >=26, after subtracting dp[i-1][s-26], cum becomes sum_{k=s-25}^{s} dp[i-1][k] mod MOD. That's the required sum (c from 0 to 25). So correct.
One nuance: The condition should be s >= 26, because when s=26, we subtract dp[i-1][0] to keep window of last 26 indices: indices 1..26? Actually let's test: at s=25, cum includes dp[i-1][0]..dp[i-1][25] (26 numbers). That's sum over c=0..25? For dp[i][25] we need sum over c=0..25 of dp[i-1][25-c] = sum_{k=0}^{25} dp[i-1][k]. That's exactly 26 terms (0 to 25). So cum at s=25 includes that. At s=26, we need sum over c=0..25 of dp[i-1][26-c] = sum_{k=1}^{26} dp[i-1][k]. That is cum at s=26 after adding dp[i-1][26] and subtracting dp[i-1][0] (since s-26=0). So correct.
Thus sliding window works.
We must also ensure dp[i-1][s] for s > (i-1)*25 are zero. That's fine.
import sys
MOD = 1000000007
MAX_N = 100
MAX_SUM = 25 * MAX_N
def precompute():
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
cum = 0
for s in range(MAX_SUM+1):
cum += dp[i-1][s]
if s >= 26:
cum -= dp[i-1][s-26]
# Keep cum modulo MOD to prevent overflow? Python ints fine, but we want dp modulo.
cum_mod = cum % MOD # Actually we can do cum %= MOD
dp[i][s] = cum_mod
return dp
But careful: cum may become negative after subtraction if we don't mod? Actually we are subtracting an integer from cum, which is sum of positive ints (dp entries are non-negative ints mod MOD). Since dp entries are non-negative, cum as sum of those is non-negative. However, we subtract dp[i-1][s-26] which was added earlier, so cum remains sum of the last up to 26 dp[i-1] values, thus non-negative. So cum will always be >=0. So we can just assign dp[i][s] = cum % MOD. But cum may be huge if we don't mod cum itself. Actually cum can become as large as sum of many dp values. For i=100, the total number of strings is 26^100, astronomically huge, far beyond Python's capacity? Python big ints can handle huge numbers, but 26^100 has about 141 digits, which Python can handle, but computing cum as exact integer might be slow due to big integer arithmetic. However, we are using dp[i-1][s] which are stored as ints modulo MOD, i.e., they are between 0 and MOD-1. So cum is sum of at most 26 such numbers, each < MOD, so cum < 26MOD ≈ 2.6e10, which fits in 64-bit. Wait but cum is built by adding dp[i-1][s] each step, but we never reset cum. Actually cum accumulates across s. At each step s, we add dp[i-1][s] to cum, and subtract dp[i-1][s-26] when s>=26. So cum is the sum of the last 26 dp[i-1] values. Since each dp[i-1][s] is a modulo value (0..MOD-1), the sum of up to 26 of them is at most 26(MOD-1) ~ 2.6e10, which fits in 64-bit. However, as we progress, cum does not accumulate all dp[i-1] values; it's a moving window sum. So cum stays bounded by 26*MOD. So we can keep cum as a normal int without modulo and just assign dp[i][s] = cum % MOD. But cum itself may be computed exactly as an integer, no overflow issues. However, when we later compute cum += dp[i-1][s], dp[i-1][s] is modulo MOD, so it's at most 1e9. So cum stays within reasonable bounds. So we can simply do:
code
cum = 0
for s in range(MAX_SUM+1):
cum += dp[i-1][s]
if s >= 26:
cum -= dp[i-1][s-26]
dp[i][s] = cum % MOD
But note: dp[i][s] is stored modulo MOD. However, for subsequent i, we will use dp[i][s] modulo MOD. That is fine because all we need is final counts modulo MOD. However, the moving window sum for the next i should also be computed using dp[i][s] modulo MOD. Since we store dp[i][s] modulo MOD, that's consistent.
Thus this method works and is efficient.
But caution: For s beyond 25i, dp[i][s] should be zero. Our method will compute some values? Let's see: For s > 25i, the actual count is zero. Our sliding window sum may produce some number? But we must ensure it yields zero. Since for i=1, dp[1][s] for s>25: cum includes dp[0][s] which are zero for s>0? Actually dp[0][0]=1, dp[0][>0]=0. So for s>25, cum will be sum of dp[0][s-25..s] all zeros because s>=26, and dp[0][k] is zero for k>0? dp[0][0]=1, but when s>=26, the window includes indices from s-25 to s, all >=1, so all zeros. So cum remains 0. So dp[1][s]=0 for s>25. Similarly for larger i, the dp[i-1][k] for k > 25*(i-1) are zero, so the window sum will be zero for s beyond 25*i. So fine.
Thus we can compute DP using sliding window.
Now, we must compute dp up to MAX_N. For each i, we loop over s up to MAX_SUM. That's 101*2501 = 252,601 iterations, each with a couple of operations. Very fast.
Now, implement precompute using this.
Now, answer each test.
Now, test with sample.
Let's manually compute dp[2][24] using this method:
dp[0][0]=1.
i=1:
cum=0
s=0: cum+=dp[0][0]=1; s<26; dp[1][0]=1%MOD=1
s=1: cum+=dp[0][1]=0 -> cum=1; dp[1][1]=1
s=2: cum+=0->1; dp[1][2]=1? Actually for s=2, cum still 1? But dp[1][2] should be 1? Wait dp[1][2] should be 1 (only if 2<=25). Yes dp[1][2]=1 because there is exactly one string of length 1 summing to 2: the letter c. So correct.
...
s=25: cum+=dp[0][25]=0 (still 1), dp[1][25]=1.
s=26: cum+=dp[0][26]=0, now s>=26, cum-=dp[0][0]=1 -> cum = -1? Wait careful: At s=26, we first cum += dp[0][26]=0, cum becomes 1 (since previous cum after s=25 was 1). Then we subtract dp[0][s-26] = dp[0][0]=1, so cum becomes 0. So dp[1][26]=0. Good.
So dp[1][s]=1 for s=0..25, 0 otherwise.
i=2:
We compute cum=0.
s=0: cum+=dp[1][0]=1 -> cum=1; dp[2][0]=1 (only "aa"? Actually strings length2 sum0: only "aa", yes 1)
s=1: cum+=dp[1][1]=1 -> cum=2; dp[2][1]=2 (strings: "ab","ba"? Actually "ab" sum 1? a=0,b=1 sum=1; "ba" sum1? b=1,a=0 sum1; also "a?" no other; yes 2)
s=2: cum+=dp[1][2]=1 -> cum=3; dp[2][2]=3? Check: strings sum2: "ac","bb","ca"? Actually a=0,c=2 sum2; b=1,b=1 sum2; c=2,a=0 sum2; also "a?"? Only those 3? Yes 3.
s=3: cum+=dp[1][3]=1 -> cum=4; dp[2][3]=4? strings sum3: "ad","bc","cb","da": 4. Good.
...
s=24: we need to get 25. Let's compute at s=24: should be 25. We'll trust.
s=25: should be 26? Actually for length2 sum25: possibilities a from max(0,25-25)=0 to min(25,25)=25 => 26 possibilities? Wait a can be 0..25 inclusive, that's 26 values. So dp[2][25]=26. But earlier for sum24 we had 25. So dp[2][25] = 26. Check: s=25, a in [0,25]? but b=25-a must be <=25, so a from 0 to 25 inclusive => 26. So dp[2][25]=26.
s=26: a from max(0,26-25)=1 to min(25,26)=25 => 25 values? Actually a can be 1..25 inclusive => 25. So dp[2][26]=25.
But does our sliding window give that? Let's simulate quickly up to s=25 for i=2.
We have dp[1][s] = 1 for s=0..25, 0 beyond.
cum starts 0.
s=0: +1 =>1, dp[2][0]=1.
s=1: +1 =>2, dp[2][1]=2.
...
s=24: after adding dp[1][24]=1, cum becomes? At s=23, cum should be sum_{k=0}^{23} dp[1][k] = 24 (since each 1). At s=24, cum += dp[1][24] => 25, dp[2][24]=25. Good.
s=25: cum += dp[1][25]=1 => 26, dp[2][25]=26.
s=26: cum += dp[1][26]=0 => still 26, then subtract dp[1][0]=1 (since s>=26) => 25, dp[2][26]=25. Good.
s=27: cum += dp[1][27]=0 => 25, subtract dp[1][1]=1 =>24, dp[2][27]=24. etc. So sliding window works.
Thus DP is correct.
Now, about modulo: Since dp values can become large, but we store mod. The sliding window uses cum which may become up to 26MOD, but we take cum % MOD for dp[i][s]. However, cum as integer might be used in next iteration without modulo; but we subtract dp[i-1][s-26] which is mod value, so cum might become negative? No, cum is non-negative because the window sum is non-negative. Actually after subtracting dp[i-1][s-26], cum might become negative if the subtraction is larger than current cum? But it shouldn't because cum before subtraction is sum of last 26 dp[i-1] values, and we subtract the oldest which is part of that sum, so cum remains sum of the other 25 values, non-negative. So cum never negative. However, if we had taken cum modulo MOD earlier, then cum might be a small number, and subtracting dp[i-1][s-26] could cause it to become negative mod-wise, but we would need to add MOD to keep it non-negative. But we are not taking modulo on cum except when storing dp[i][s]. So we keep cum as exact integer (not modulo). So we must ensure that cum stays as exact sum of the window of dp[i-1] values (which are stored as ints between 0 and MOD-1, but these are the actual counts modulo MOD? Wait, dp[i-1][k] is stored as the count modulo MOD, not the exact count. So when we sum them, we are summing modulo residues. The sum of residues modulo MOD is congruent to the true sum modulo MOD. Since we only care about the result modulo MOD, it's fine to sum residues. However, the moving window sum cum = sum_{k in window} dp[i-1][k] (mod residues). Since each dp[i-1][k] is between 0 and MOD-1, cum is an integer between 0 and 26(MOD-1). That integer is congruent to the true sum modulo MOD. When we assign dp[i][s] = cum % MOD, we are storing the correct modulo.
But is it valid to use the modulo values in the cumulative sum for subsequent i? Yes, because we only need final results modulo MOD. The recurrence is linear, so working modulo MOD throughout is fine. However, careful: The moving window relies on the fact that dp[i-1][k] are the actual counts modulo MOD. That's fine because we are computing dp[i][s] modulo MOD. So using residues is correct.
Now, for the window sum, we need to subtract the value that leaves the window. If we keep cum as an integer that is the sum of residues (not modulo), then after subtracting, cum remains the sum of the new window residues. That is fine. However, if we had taken cum modulo MOD after each step, then subtraction might not correspond to the actual sum of residues because the modulo operation loses information about the magnitude, which could lead to incorrect subtraction when the window wraps around? But if we always keep cum modulo MOD, we must also subtract modulo MOD, and the result will still be correct modulo MOD because (a+b) mod M = (a mod M + b mod M) mod M, and subtraction similar. So we could keep cum modulo MOD as well. That would be simpler: we can do cum = (cum + dp[i-1][s]) % MOD, and if s >= 26: cum = (cum - dp[i-1][s-26]) % MOD. Then assign dp[i][s] = cum (which is already modulo). But we must ensure that subtraction yields non-negative modulo: (cum - dp[i-1][s-26]) % MOD works in Python (produces result in [0, MOD-1]) because % yields non-negative remainder. So we can keep cum modulo MOD.
Let's test: With dp[1][s] as 0 or 1 mod MOD.
i=2:
cum=0
s=0: cum = (0+1)%MOD =1; dp[2][0]=1
s=1: cum = (1+1)%MOD =2; dp[2][1]=2
s=2: cum = (2+1)=3; dp[2][2]=3
...
s=25: cum = (24+1)%MOD? Actually at s=24 cum=25; s=25: cum = (25+1)=26; dp[2][25]=26.
s=26: cum = (26+0)=26; then subtract: cum = (26 - dp[1][0]) % MOD = (26-1)%MOD=25; dp[2][26]=25.
That works. So we can keep cum modulo MOD.
But careful: At s=26, cum before subtraction is 26. If we had taken modulo earlier, it's still 26 (< MOD). So fine.
Thus we can implement as:
code
cum = 0
for s in range(MAX_SUM+1):
cum = (cum + dp[i-1][s]) % MOD
if s >= 26:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
But note: For s=0..25, we never subtract. So dp[i][s] = sum_{k=0}^{s} dp[i-1][k] mod MOD. For s>=26, dp[i][s] = sum_{k=s-25}^{s} dp[i-1][k] mod MOD. Good.
Thus implementation is simple and efficient.
Now, ensure that dp[0][0] = 1, others 0.
Now, we should also ensure that for i >=1, dp[i][s] for s > 25*i is computed correctly as 0. Let's test: For i=1, s>25: dp[0][s] are 0, so cum remains 0; also no subtraction for s<26? Actually for s>25, s>=26 so we subtract dp[0][s-26]. But cum is 0, and we subtract something (which is 0 for s-26>0? For s=26, s-26=0, dp[0][0]=1, but cum is 0 from adding dp[0][26]=0. So cum = (0 - 1) % MOD = MOD-1? That would be wrong! Wait, check: For i=1, we are computing dp[1] from dp[0]. At s=26:
cum initially 0. At s=26, we do cum = (cum + dp[0][26]) % MOD = (0+0)=0.
Since s>=26, we then do cum = (cum - dp[0][0]) % MOD = (0 - 1) % MOD = MOD -1 (i.e., 1e9+6). Then assign dp[1][26] = MOD-1, which is wrong; it should be 0. So the sliding window method with modulo and subtraction after addition fails for the first row? Let's analyze. The issue: The subtraction should be done before or after? In the moving window, the correct order is: add new element, then if window size exceeds 26, subtract the element that fell out. But our cum variable is supposed to represent the sum of the current window (last up to 26 elements). We need to maintain the window correctly. At step s, after adding dp[i-1][s], the window includes indices from max(0, s-25) to s. For s=26, after adding dp[i-1][26], the window should include indices 1..26. That means we need to remove index 0. So we subtract dp[i-1][0] after adding. That's what we did. However, cum before addition for s=26 should have been sum of indices 0..25. At s=25, cum = sum_{k=0}^{25} dp[i-1][k] = 1 (since only dp[0][0]=1, others 0). So cum=1. Then at s=26, we first add dp[0][26]=0, so cum becomes 1. Then subtract dp[0][0]=1, cum becomes 0. That's correct. So the issue arises because we reset cum to 0 at the beginning of the loop and then for each s we do cum = (cum + dp[i-1][s]) % MOD. But that assumes cum from previous s is kept. In my description above, I said we keep cum across iterations. So we must not reset cum inside the loop; we keep it from previous s. In my pseudo-code, I had cum = 0 before the loop, then inside the loop we update cum accordingly, but we didn't reset each time. So the correct code is:
code
cum = 0
for s in range(MAX_SUM+1):
cum += dp[i-1][s]
if s >= 26:
cum -= dp[i-1][s-26]
dp[i][s] = cum % MOD
That's the version without modulo on cum. Or with modulo:
code
cum = 0
for s in range(MAX_SUM+1):
cum = (cum + dp[i-1][s]) % MOD
if s >= 26:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
But this version resets cum each iteration? No, cum persists across iterations. In the loop, we update cum using previous cum. That's fine. However, for i=1, at s=26, what is cum before the iteration? It should be the sum after processing s=25. Let's simulate using modulo version:
Initialize cum=0.
s=0: cum = (0 + dp[0][0]=1) % MOD = 1; s<26; dp[1][0]=1.
s=1: cum = (1 + dp[0][1]=0) = 1; dp[1][1]=1.
...
s=25: cum = (previous cum? after s=24, cum = 1? Actually careful: dp[0][k] for k=1..24 are 0. So cum after s=0:1; after s=1: (1+0)=1; after s=2:1; ... after s=24:1; after s=25: cum = (1 + dp[0][25]=0)=1; dp[1][25]=1. Good.
s=26: cum = (1 + dp[0][26]=0) = 1; now s>=26, cum = (1 - dp[0][0]=1) % MOD = 0; dp[1][26]=0. Good! So the modulo version works as long as we don't reset cum each iteration. In my earlier erroneous simulation, I mistakenly thought cum was reset to 0 each s. But in code, cum persists. So it's fine.
But careful: In Python, when we do cum = (cum - dp[i-1][s-26]) % MOD, note that the subtraction may give negative, but % MOD yields positive. That's fine.
Thus sliding window works correctly with modulo.
Now, but we must be careful that after subtraction, cum becomes the sum of the window (which is non-negative). With modulo, it becomes the correct residue. So we can use modulo version to keep numbers small.
Thus I'll implement sliding window with modulo:
code
def precompute():
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
cum = 0
for s in range(MAX_SUM+1):
cum = (cum + dp[i-1][s]) % MOD
if s >= 26:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
return dp
But is it correct that for s from 0 to MAX_SUM, we always add dp[i-1][s] first, then subtract if needed? That yields window from s-25 to s. Yes.
Now, test for i=2, s=26 using this method: We need to check that cum after s=25 is sum_{k=0}^{25} dp[1][k] = 26? Actually dp[1][k] for k=0..25 = 1 each, so sum = 26. But our cum after s=25 should be 26. Let's simulate:
i=2, dp[1][k] are 1 for 0..25, 0 beyond.
cum starts 0.
s=0: cum = (0+1)=1; s<26 => dp[2][0]=1.
s=1: cum = (1+1)=2; dp[2][1]=2.
...
s=24: cum should be 25; dp[2][24]=25.
s=25: cum = (25+1)=26; dp[2][25]=26.
s=26: cum = (26+0)=26; then subtract dp[1][0]=1 => cum = (26-1)%MOD=25; dp[2][26]=25.
Works.
Now, what about s beyond 25i? For i=2, at s=51 (max sum 50 actually for i=2? Actually 252=50, so s=51 >50 should be 0. Let's test s=51:
At s=51, cum before addition: from previous s=50, cum =? We need to ensure that cum after s=50 is correct and then s=51 will become 0. Let's not test manually, but algorithm should produce 0 because the window will include dp[1][26..51]? Actually dp[1][k] for k>25 are 0, so the window sum will eventually become 0 after enough steps where all dp[1] in window are zero. Because after s=50, the window includes indices 25..50? Wait at s=50, window = s-25=25 to 50 inclusive. Those are indices 25 (dp[1][25]=1) and 26..50 (all 0). So sum =1. At s=51: add dp[1][51]=0 => cum=1; subtract dp[1][26]=0 => cum=1? That would give dp[2][51]=1, which is wrong because max sum for length 2 is 50. So something is off: For i=2, s=51 is impossible; dp[2][51] should be 0. Let's simulate properly using sliding window.
Important: Our DP recurrence: dp[i][s] = sum_{c=0}^{25} dp[i-1][s-c] (with dp[i-1][negative]=0). For s > 25i, this sum will be zero because s-c > 25(i-1) for all c <=25? Not exactly: If s = 25i + d, then s-c >= 25i + d -25. Since dp[i-1][t] is zero for t > 25*(i-1). But 25i + d -25 = 25(i-1) + d. For d>0, this is > 25*(i-1), so dp[i-1][t]=0 for all t in that range because t > 25*(i-1). So sum = 0.
Now, does the sliding window correctly handle this? Let's check with i=2, s=51.
We need dp[2][51] = sum_{c=0}^{25} dp[1][51-c]. For c from 0..25, 51-c ranges from 51 down to 26. All these indices are >25, so dp[1][k] = 0 for k>25. So sum = 0. So correct.
Now, does our sliding window compute that? At step s=51, after adding dp[1][51] (which is 0) and subtracting dp[1][25] (since s-26=25), what is cum? To know cum, we need to know cum before addition at s=51. cum is supposed to be the sum of dp[1][s-25 .. s] after processing s. Let's track cum from earlier.
We need to compute cum for i=2 across s. Let's compute dp[1][k] for k=0..25 =1, k>25=0.
We'll simulate the sliding window for i=2 up to s=51.
Initialize cum=0.
We'll compute cum after each step:
s=0: cum = 0+dp[1][0]=1 -> cum=1; (s<26 no sub). dp[2][0]=1.
s=1: cum=1+1=2; dp[2][1]=2.
s=2: cum=2+1=3; dp[2][2]=3.
...
s=25: cum=25+1=26; dp[2][25]=26.
s=26: cum=26+dp[1][26]=26+0=26; subtract dp[1][0]=1 => cum=25; dp[2][26]=25.
s=27: cum=25+dp[1][27]=25+0=25; subtract dp[1][1]=1 => cum=24; dp[2][27]=24.
...
Continue. At each step from s=26 onward, we subtract dp[1][s-26] which is 1 for s-26 <=25. So for s from 26 to 50, s-26 ranges from 0 to 24. Those are all indices where dp[1][index]=1. For s=50, s-26=24, so subtract 1. Let's compute cum at s=50.
We can derive a formula: For s between 26 and 50 inclusive, cum = 26 - (s-25)? Actually let's compute pattern:
At s=26, cum=25 = 26-1.
s=27, cum=24 = 26-2.
...
So at s=26 + t, where t from 0 to 24, cum = 26 - (t+1)? Let's check: t=0: cum=25 =26-1; t=1: cum=24=26-2; so at s=26+t, cum = 26 - (t+1) = 25 - t? Actually 26 - (t+1) = 25 - t. But we have cum=25 at t=0, 24 at t=1, so yes cum = 25 - t. But note 25 - t = 26 - (t+1). So at s = 26 + t, cum = 25 - t. When t=24 (s=50), cum = 25 - 24 = 1. So at s=50, cum should be 1.
Let's manually compute: At s=50, window indices from s-25=25 to s=50 inclusive. That's indices 25 (value 1) and 26..50 (values 0). So sum = 1. Good.
Now s=51:
Add dp[1][51]=0 -> cum remains 1 (from previous cum=1).
Since s>=26, subtract dp[1][s-26] = dp[1][25] = 1. So cum = (1 - 1) % MOD = 0. So dp[2][51] = 0. Good! So sliding window yields 0.
So it works.
Thus sliding window method is correct.
Now, one more check: for s=52, window indices 27..52 all zeros, cum should be 0. Starting cum=0 from previous step, add 0 -> 0, subtract dp[1][26]=0 ->0. So fine.
Thus algorithm works for all s.
Now, we should ensure that for i=1, the window works as well. We already simulated s=26 gave 0.
Now, we need to store dp for all n up to 100. So sliding window DP is efficient.
Now, implement.
Now, final code with precompute and solve.
Potential pitfall: Input may have trailing spaces or blank lines; using sys.stdin.read().split() handles that.
Now, test with given examples using mental or actual run? I trust.
Now, consider maximum n=100, MAX_SUM=2500. The DP loops 101 * 2501 ≈ 252k iterations, each with a couple of operations. That's negligible.
Now, code.
One more thing: When we compute answer, we do (dp[n][S] - 1) % MOD. Since dp[n][S] is already modulo, subtracting 1 may give negative, so we add MOD if needed. Use (dp[n][S] - 1) % MOD, but in Python, negative % MOD yields positive remainder, but it's slower maybe. Simpler: ans = dp[n][S] - 1; if ans < 0: ans += MOD. Since dp[n][S] is at least 1, ans >=0? Actually dp[n][S] could be 0? No, for a valid S, it's at least 1. So ans >=0. But dp[n][S] is mod MOD, so if dp[n][S] is 0 mod MOD, that would mean actual count is multiple of MOD, and then dp[n][S] - 1 = -1 mod MOD = MOD-1. But can dp[n][S] be 0 modulo MOD even though actual count >0? Yes, if the exact count is divisible by MOD. For example, if the count is exactly MOD, then dp[n][S] = 0 (mod MOD). Then answer should be (MOD-1) mod MOD. So we must handle that case. So we should compute (dp[n][S] - 1 + MOD) % MOD. That's safe. Because dp[n][S] is stored modulo MOD, so we don't know if it's actually 0 but count is positive. So (dp[n][S] - 1) % MOD works correctly in Python because negative modulo yields positive remainder. So we can do ans = (dp[n][S] - 1) % MOD.
Thus final.
Now, we should also consider that the problem might have multiple test cases and we need to output each answer.
Now, I'll produce final code.
But before final, let's test with a small case manually using DP code mentally.
Case: n=1, s="a" => S=0, dp[1][0]=1, ans=0. OK.
Case: n=2, s="zz"? Actually 'z'=25, so sum=50. Number of strings length2 sum50: a from max(0,50-25)=25 to min(25,50)=25, so only a=25,b=25 => "zz". So dp[2][50]=1, ans=0. That makes sense: from "zz", you cannot perform any operation because you cannot increase 'z' or decrease 'a'? Actually from "zz", can we do any operation? p=1: try to replace s1 with following (z->? not allowed) or preceding (z->y) and s2 with following (z->? not allowed) or preceding? Both options require either increment or decrement of a 'z' or 'a'? Let's check: Option1: s1 following (invalid because z), so cannot. Option2: s1 preceding (y) and s2 following (invalid because z has no following). So cannot. So only itself. So ans=0, matches.
Case: n=2, s="az": sum=0+25=25. Number of strings sum25: a from max(0,25-25)=0 to min(25,25)=25 => 26 possibilities. So class size 26. Can we reach all? Likely yes. So ans=25.
Now, implement code.
One more check: For n=100, S=2500, dp[100][2500] could be computed.
Now, note: The DP sliding window uses dp[i-1][s] for s up to MAX_SUM. Since dp[i-1][s] for s > 25*(i-1) are zeros, but we still access them. That's fine.
Now, let's write final solution in Python.
I'll include comments.
Potential improvements: Use list comprehension for dp as list of lists.
Now, final answer.
But wait: The problem's input contains up to 10^4 tests, and we need to output each answer. Using sys.stdout.write at end is fine.
Now, code:
code
import sys
MOD = 1000000007
MAX_N = 100
MAX_SUM = MAX_N * 25 # 2500
def precompute_dp():
dp = [[0]*(MAX_SUM+1) for _ in range(MAX_N+1)]
dp[0][0] = 1
for i in range(1, MAX_N+1):
cum = 0
for s in range(MAX_SUM+1):
cum = (cum + dp[i-1][s]) % MOD
if s >= 26:
cum = (cum - dp[i-1][s-26]) % MOD
dp[i][s] = cum
return dp
def solve():
dp = precompute_dp()
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
word = data[idx]
idx += 1
n = len(word)
total = sum(ord(ch) - 97 for ch in word)
ans = (dp[n][total] - 1) % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
Test with sample.
Now, consider if there are any words with uppercase? No, only lowercase.
Now, final answer.
But before final, let's double-check the sliding window logic for correctness when s < 26 and i>1. For i=2, s=0..25, we never subtract, so cum = sum_{k=0}^{s} dp[1][k]. Since dp[1][k] is 1 for k<=25, cum = s+1. That matches dp[2][s] = s+1? Actually dp[2][s] = number of strings length2 sum s, which is s+1 for s<=25. Yes, because a can be 0..s. So correct.
Now, for i=3, we can trust.
Thus solution is correct.
Now, confirm with sample 3: we already computed manually using inclusion-exclusion gave correct N and answer. Our DP should produce the same.
Thus answer is ready.
Now, discuss complexity: Precomputation O(MAX_N * MAX_SUM) = 2.5e5, queries O(total length) which is up to 1e6. So fast.
Now, I'll produce final answer with explanation.
But note: The problem statement says "print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words." That is exactly what we computed.
Now, finalize.
We should also consider that some words might have the same length and sum as themselves, obviously. The answer is count-1.
Now, ensure that dp array is computed modulo MOD. Since answer required modulo.
Now, I'll write final solution in the answer box. ```python
import sys
def precompute():
"""dp[i][s] = number of strings of length i with sum s (mod MOD)"""
dp = [[0] * (MAX_SUM + 1) for _ in range(MAX_N + 1)]
dp[0][0] = 1
for i in range(1, MAX_N + 1):
window_sum = 0
for s in range(MAX_SUM + 1):
window_sum = (window_sum + dp[i-1][s]) % MOD
if s >= 26:
window_sum = (window_sum - dp[i-1][s-26]) % MOD
dp[i][s] = window_sum
return dp
def solve() -> None:
dp = precompute()
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
word = data[idx]
idx += 1
n = len(word)
total = sum(ord(ch) - 97 for ch in word) # a -> 0, b -> 1, ...
ans = (dp[n][total] - 1) % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))