When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to di. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most x from the root. The distance is the sum of the lengths of edges on the path between nodes.
But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' — you may ask. It's just that he feels superior knowing that only he can solve this problem.
Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 109 + 7.
Input
The first line contains two space-separated integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 109) — the number of children of each node and the distance from the root within the range of which you need to count the nodes.
The next line contains n space-separated integers di (1 ≤ di ≤ 100) — the length of the edge that connects each node with its i-th child.
Output
Print a single number — the number of vertexes in the tree at distance from the root equal to at most x.
Examples
Input
3 3
1 2 3
Output
8
Note
Pictures to the sample (the yellow color marks the nodes the distance to which is at most three)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to count number of nodes (including root) in an infinite rooted tree where each node has exactly n children, and the edge length to i-th child is d_i (for i=1..n). The root is at distance 0. We need count of nodes with distance <= x.
The tree is infinite, but we only count nodes up to distance x. Since distances are positive (>=1), the tree up to depth D is finite because edge lengths positive. So we can count nodes in the tree within distance x.
This is essentially counting number of paths (including empty path) from root where sum of edge lengths along path <= x, where at each step we choose a child i and add d_i.
This is like: number of sequences (i1, i2, ..., ik) with k >=0 such that sum_{j=1..k} d_{i_j} <= x. Each sequence corresponds to a node. So the answer is number of ways to write a sum <= x using an ordered list of positive integers (d_i) from a set of n values (with repetitions allowed). That's like the number of words over alphabet of size n with weighted length constraint.
We need to compute the total number of such sequences mod M=1e9+7, where n up to 1e5, x up to 1e9, d_i up to 100.
This is similar to counting number of ways to partition something with coins but ordered (compositions). Equivalent to number of paths in a DAG from root to nodes with total cost <= x. Could be computed via DP: Let f(t) = number of sequences (including empty) with total sum exactly t. Then answer = sum_{t=0}^x f(t). f(0)=1 (empty). For t>0, f(t) = sum_{i=1}^n f(t - d_i) (if t >= d_i). This is like linear recurrence. Since d_i are up to 100, we can consider generating function: F(z) = 1 / (1 - sum_{i=1}^n z^{d_i})? Actually number of sequences of any length with sum exactly t corresponds to the coefficient in generating function: sum_{k>=0} (sum_i z^{d_i})^k = 1/(1 - S(z)) where S(z) = sum_i z^{d_i}. Indeed, each sequence contributes product of z^{d_i}. So f(t) is coefficient of z^t in 1/(1 - S(z)). So the generating function is rational where denominator is 1 - S(z). But S(z) is a polynomial of degree max(d) (<=100). So we can use linear recurrence of order max(d) (actually up to max_d) but there is a twist: the recurrence is f(t) = sum_{i=1}^n f(t - d_i) for t > 0. That's valid because any sequence ending with step d_i: preceding sum = t - d_i. So yes. This is a linear recurrence with constant coefficients. However, n can be large (1e5) but each term involves summing over all n children; but many d_i may be repeated. Since d_i <= 100, we can compress: let c[d] = number of children with edge length d, for d from 1 to 100. Then f(t) = sum_{d=1}^{100} c[d] * f(t-d). That's a recurrence of order 100, with coefficients c[d] (nonnegative integers). That's manageable. So we need sum_{t=0}^x f(t) mod MOD.
x up to 1e9, so we need to compute this sum efficiently using matrix exponentiation or linear recurrence techniques (like linear recurrences via exponentiation of companion matrix, or using generating functions + polynomial methods). Since order m = 100, we can use fast linear recurrence using matrix exponentiation of size m, but x up to 1e9 -> O(m^3 log x) is 1e6 * log? Actually m=100 => m^3 = 1e6, log x ~30 => 3e7 operations, which might be okay in C++ but Python might be heavy. However we can optimize using linear recurrences with exponentiation via exponentiation of polynomial or using doubling method (like linear recurrence using Kitamasa algorithm O(m^2 log x) or O(m log m log x) using FFT but m small). For m=100, O(m^2 log x) is 10000 * 30 = 300k, which is very light in Python. So we can implement Kitamasa method: given recurrence f(n) = sum_{i=1}^m c_i f(n-i) for n>=m, with initial f(0..m-1) known, compute f(n) quickly. Then we need sum of f(t) for t up to x. We could compute cumulative sum using similar recurrence? There is technique: define g(t) = sum_{i=0}^{t} f(i). Then answer = g(x). We can derive recurrence for g as well: Since f(t) = g(t)-g(t-1). But maybe we can incorporate into a larger linear recurrence. Alternatively, we can compute f(t) for t up to something? x is huge, so not.
We can use matrix exponentiation with state vector containing recent f values and the cumulative sum. For recurrence order m=100, we can create state vector of size m+1: (f(t), f(t-1), ..., f(t-m+1), S(t)) where S(t) = sum_{i=0}^t f(i). Then we want to transition to t+1. Transition: f(t+1) = sum_{i=1}^m c_i * f(t+1-i). But the state contains f(t), f(t-1), ..., f(t-m+1). To compute f(t+1), we need f(t), f(t-1),..., f(t-m+1) (all known). Then we can update the shift and S(t+1) = S(t) + f(t+1). So we can define a (m+1) x (m+1) matrix that does this. Then we can compute the state at x by matrix exponentiation from initial state (t=m-1 maybe?). However we need to handle base cases for t < m. Since recurrence holds for t >= m? Actually the recurrence f(t) = sum_{d} c[d] f(t-d) is valid for t > 0? Let's verify: For t>0, f(t) = sum_{d} c[d] * f(t-d) as long as we consider all t, but note that for t < d, f(t-d) is zero? Actually f(t) for negative t is 0 by definition (no negative sum). So we can define f(t) for t >= 0 with base f(0)=1, and recurrence f(t) = sum_{d=1}^{100} c[d] f(t-d) for t>=1, with the convention f(negative)=0. That works like linear recurrence of order max_d=100, but coefficients c[d] for all d. However, the recurrence might not hold for t<max_d? Actually for t=1, f(1) = sum_d c[d] f(1-d) = sum_{d=1}^{100} c[d] f(negative) + c[1] f(0) if d=1 exists. Since f(negative)=0, we get f(1) = c[1] * f(0) if 1 in d else 0. That's correct: f(1) counts ways to sum to 1 using one step of length 1; but there might also be ways with multiple steps? No, because we have recurrence: any sequence sum to t ends with step d, preceding sum t-d. So indeed f(t) = sum_d c[d] f(t-d) for t>=1, with f(0)=1, f(neg)=0. So it's a valid recurrence for all t>=1. That's a linear recurrence of order 100 but with the base case f(0)=1 and f(t)=0 for t<0. However, it's not homogeneous? It is homogeneous except the initial condition? Actually it's homogeneous: f(t) - sum c[d] f(t-d) = 0 for t>=1. But f(0)=1 is a boundary. The recurrence alone would produce zero for all if we start with zero initial? But we have f(0)=1 and for t<0 f(t)=0, so it's a linear recurrence with that condition.
So we can compute f(t) for any t using linear recurrence with order m = max_d (maybe we can compress to m = max(d) that have c[d]>0? But maximum d up to 100). However, the recurrence involves f(t-d) for d up to 100. So the state vector for the recurrence needs to contain the last 100 values of f: f(t), f(t-1), ..., f(t-99). But we also have c[d] for d up to 100. So the companion matrix is 100x100. That is manageable. But we also need cumulative sum S(t). So state size 101. Then we can compute from t=0 up to x using matrix exponentiation.
But careful: The recurrence holds for all t>=1, but the state at t=0 would be: we need to define f for indices maybe negative? Better approach: We can initialize state vector for t from 0 to 99? Actually for matrix exponentiation we need a state that contains the last 100 values of f at some step. Let's define for each integer t >= 0, we have a state vector v_t = [f(t), f(t-1), ..., f(t-99)]^T, where for negative indices f(neg)=0. So for t from 0 to 99, some entries are defined as 0. Then we want to transition to v_{t+1} = M * v_t, where M is a 100x100 matrix. Specifically, v_{t+1}[0] = f(t+1) = sum_{d=1}^{100} c[d] * f(t+1-d) = sum_{d=1}^{100} c[d] * v_t[d-1]? Let's map: v_t = (f(t), f(t-1), ..., f(t-99)). So f(t+1-d) corresponds to: if we let i = d-1, then f(t+1-d) = f(t - (d-1))? Wait: f(t+1-d) = f(t - (d-1)). For d=1: f(t) = v_t[0]. For d=2: f(t-1) = v_t[1]. For d=100: f(t-99) = v_t[99]. So indeed f(t+1) = sum_{d=1}^{100} c[d] * v_t[d-1]. So first row of M consists of coefficients: for column j (0-indexed), coefficient = c[j+1] if j+1 is a d, else 0. So M[0][j] = c[j+1] (since j from 0 to 99 corresponds to d=j+1). The other rows: shift: v_{t+1}[1] = f(t) = v_t[0]; v_{t+1}[2] = f(t-1) = v_t[1]; ... v_{t+1}[99] = f(t-98) = v_t[98]. So M is like companion: row i+1 has 1 at column i for i=0..98, rest zeros. So M is 100x100.
Now the cumulative sum S(t) = sum_{i=0}^t f(i). We can extend state to include S(t). Then v'_t = [f(t), f(t-1), ..., f(t-99), S(t)]^T of size 101. Transition to t+1:
f(t+1) as above, using f(t),...,f(t-99).
Then S(t+1) = S(t) + f(t+1).
And shift of f values: new f(t+1) becomes first element, previous f(t) becomes second, etc.
So we can define M' of size 101x101. First row: M'[0][0..99] = c[1..100] (coefficients), and M'[0][100] = 0. Row i for i=1..98? Actually we have 100 f slots, indices 0..99 for current state. After shift, slot1 gets old slot0, slot2 gets old slot1, ..., slot99 gets old slot98. slot100 (the cumulative sum) gets updated. So rows:
For i from 1 to 99 (shifting): row i has a 1 at column i-1, zeros elsewhere.
Row for cumulative sum (index 100): we need to compute S(t+1) = S(t) + f(t+1). f(t+1) is computed from first row. So row 100 has: it should take the sum of previous S(t) and the new f(t+1). So row 100 has: coefficients for f entries = same as first row (c[1..100])? Actually S(t+1) = S(t) + sum_{d} c[d] f(t+1-d) = S(t) + sum_{j} c[j+1] v_t[j]. So row 100 should have: column j (0..99): c[j+1]; column 100: 1 (to keep S(t)). And also maybe? But careful: we are writing v'_{t+1} = M' * v'_t. So each component is linear combination of components of v't. So row 100: v'{t+1}[100] = 1 * v't[100] + sum{j=0}^{99} c[j+1] * v'_t[j]. That's correct.
Thus M' is constructed.
Now we need to get S(x) as the last component of v'_x, starting from initial state v'_0? But v'_0 corresponds to t=0? Let's define initial time t=0. Then v'_0 should contain f(0), f(-1),..., f(-99), S(0). f(0)=1, f(neg)=0, S(0)=1 (since sum includes root). So v'_0 = [1, 0, 0, ..., 0 (for f(-1)..f(-99)), 1]^T. However, the state representation we defined: at time t, we store f(t), f(t-1), ..., f(t-99). For t=0, this is [f(0), f(-1), ..., f(-99)] = [1,0,...,0]. So that's fine. And S(0)=1.
We need to compute v'_x = (M')^x * v'_0? Wait, careful: our transition M' advances time by 1 (t -> t+1). So applying M' once from state at t yields state at t+1. So starting from v'_0, applying M' x times yields v'_x. So we need to compute v'_x = (M')^x * v'_0. Then the answer is v'_x[100] (the cumulative sum). So we can compute matrix power exponentiation modulo MOD.
Matrix size 101x101. Exponentiation O(size^3 log x) = 101^3 ≈ 1,030,301, times log2(1e9)~30 => ~30 million multiplications modulo MOD. Each multiplication is constant time, but in Python, this could be borderline but maybe feasible with optimization? 30 million operations of 64-bit modular arithmetic might be okay (like 0.1-0.2 secs per million? Actually Python is slower. 30 million could be ~1-2 seconds? But matrix multiplication naive O(n^3) is 1e6 per multiplication, times 30 = 30e6 inner loops? Actually for each multiplication of two 101x101 matrices, naive triple loop: 101^3 = ~1,030,301 multiply-add operations. 30 such multiplications = ~31 million. That's not too bad, but we also need to multiply matrix by vector (101^2). So total maybe 31 million mod operations. Python can handle about 50 million simple operations per second? Actually Python is slower: ~10-20 million operations per second maybe? 31 million might be ~2-3 seconds. But we need to be careful with overhead. Could be acceptable in Codeforces typical limits? Possibly but we can try to optimize by using numpy? Not allowed. But we can improve by using exponentiation with doubling and maybe use faster methods for linear recurrence like linear recurrences with cumulative sum trick using Kitamasa. That might be simpler and faster.
Alternative: Since recurrence order m=100, we can compute f(t) for t up to x using linear recurrence in O(m^2 log x) via Kitamasa. But we also need the cumulative sum S(x). We can compute f(t) for any t, and then we could compute sum via another linear recurrence or via prefix sums using matrix exponentiation of size m+1. But perhaps we can compute S(x) directly using Kitamasa as well by constructing a linear recurrence for S(t). Note that f(t) satisfies recurrence f(t) = sum_{d} c[d] f(t-d). Then S(t) = S(t-1) + f(t). Then we can derive a recurrence for S(t) of order m+1 maybe. Write S(t) - S(t-1) = f(t) = sum_{d} c[d] f(t-d) = sum_{d} c[d] (S(t-d) - S(t-d-1)). So S(t) - S(t-1) = sum_{d=1}^m c[d] (S(t-d) - S(t-d-1)). Rearranged: S(t) = S(t-1) + sum_{d} c[d] S(t-d) - sum_{d} c[d] S(t-d-1). This is a linear recurrence of order m+1? Let's try to combine: For each d, we have terms S(t-d) and S(t-d-1). So the highest index is t-1 (from S(t-1)) and t-d (for d=1 gives t-1) and t-d-1 for d=1 gives t-2; the maximum order is t-1 maybe? Actually the highest lag is for d=1: S(t-1) appears; also S(t-1) appears from first sum? Let's re-index: Let k = t-1. But we can write recurrence for S(t) in terms of S(t-1), S(t-2), ..., S(t-m-1). Indeed, for d=m, we have S(t-m) and S(t-m-1). So order m+1. However we might be able to reduce it because f(t) recurrence order m gives that the generating function of S includes denominator (1-z)(1 - S(z))? Actually F(z) = 1/(1 - P(z)) where P(z)=sum c[d] z^d. Then S cumulative sum generating function G(z) = F(z) / (1 - z) = 1/[(1 - z)(1 - P(z))]. So denominator polynomial is (1 - z)(1 - P(z)) = (1 - z) - (1 - z)P(z)? Actually compute: (1 - z)(1 - P(z)) = 1 - z - P(z) + z P(z). But as a polynomial, it's of degree max(m,1) + 1 = m+1 (since P(z) has degree m). So indeed G(z) satisfies a linear recurrence with order m+1. So we could compute S(t) directly via linear recurrence of order m+1 using Kitamasa. But we need the coefficients of the recurrence for S. Alternatively, we can use the matrix method but with size m+1=101 (same). So essentially similar complexity.
But we can also compute using DP with generating function and exponentiation by squaring of polynomial? Not necessary.
Given m=100, using matrix exponentiation of size 101 is acceptable. But we can try to implement a fast linear recurrence using exponentiation of companion matrix with exponentiation by squaring but using linear transformations represented as matrices but we could use exponentiation of linear recurrences via polynomial exponentiation (Kitamasa) which is O(m^2 log x) for a single term. For cumulative sum, we can adapt: we want S(x). We can compute f(x) and also maybe compute sum via prefix sum recurrence: let g(t) = S(t). Then we have recurrence: g(t) = g(t-1) + f(t). But we can combine into a 2D state (f values). Actually we can compute f(t) via Kitamasa, but to get sum we would need to compute sum of f from 0..x, which might be computed using the formula sum_{t=0}^x f(t) = coefficient of something? Maybe we can use the generating function: G(z) = 1/((1-z)(1-P(z))). We could compute the coefficient of z^x in G(z) modulo something? That is essentially S(x). But computing coefficient of x up to 1e9 is similar to linear recurrence.
Alternatively, we can compute using matrix exponentiation with size 101. I think it's fine.
But we need to be careful about memory and speed. 101x101 matrix multiplication naive O(n^3) with Python ints mod. Let's estimate more accurately: 101^3 = 1,030,301 multiplications and additions. In Python, each operation might be ~0.1 micro? Actually Python integer multiplication modulo is slower. But 1 million operations per matrix multiplication might be ~0.05 sec? Actually testing: 1 million simple integer operations in Python can be ~0.02-0.05 sec? Not sure. But 30 million could be 1-2 secs. Might be okay. However, we also need to handle multiple test cases? The problem is single test case. So likely okay.
But we can further reduce the matrix size: Since we only need cumulative sum, we can incorporate cumulative sum into recurrence of order m+1, but it's still 101. Alternatively, we could compute f(t) using linear recurrence and then compute prefix sums via another linear recurrence using the same method but of order m+1. That would be O((m+1)^2 log x) which is ~10k * 30 = 300k, which is much faster. Let's do that: We'll derive coefficients for recurrence for S(t) directly.
We have f(t) recurrence: f(t) = sum_{d=1}^{m} c[d] f(t-d) for t>=1, with f(0)=1, f(neg)=0.
Define S(t) = sum_{i=0}^t f(i). We want S(x). Let's find linear recurrence for S.
From f(t) = S(t) - S(t-1) for t>=1, and define S(-1)=0 for convenience (since sum from 0 to -1 empty). Then for t>=? We need recurrence for S(t) for t sufficiently large.
S(t) = (1 + c[1]) S(t-1) + sum_{d=2}^{m} (c[d] - c[d-1]) S(t-d) - c[m] S(t-(m+1)), for t >= something? Need to check base validity: For t large enough so that all indices are >=0? But recurrence should hold for all t >= m+1? Let's test for small m. Actually the original f recurrence holds for t>=1. The derived recurrence should hold for t >= m+1? Because we need S(t-(m+1)) non-negative? But for t < m+1, some terms involve negative indices. However we can treat S(negative) as 0? For t small, we would need to use base values. For recurrence method we can compute S(t) for t up to x using linear recurrence of order m+1, but we need initial values S(0), S(1), ..., S(m) maybe? Then recurrence holds for t > m? Let's verify for t = m+1: Does it hold? Might hold if we define S(negative)=0. We can check with simple example.
Better approach: we can compute recurrence for S directly from generating function: G(z) = 1/( (1-z)(1-P(z)) ). Multiply both sides: (1-z)(1-P(z)) G(z) = 1. Expand: (1 - z - P(z) + z P(z)) G(z) = 1. So the denominator polynomial Q(z) = (1 - z)(1 - P(z)) = 1 - z - P(z) + z P(z). Write P(z) = sum_{d=1}^m c[d] z^d. Then Q(z) = 1 - z - sum_{d=1}^m c[d] z^d + z * sum_{d=1}^m c[d] z^d = 1 - z - sum_{d=1}^m c[d] z^d + sum_{d=1}^m c[d] z^{d+1} = 1 - z + sum_{d=1}^m c[d] (z^{d+1} - z^d). So Q(z) = 1 - z + sum_{d=1}^m c[d] z^{d+1} - sum_{d=1}^m c[d] z^d.
Combine constant terms: Only constant is 1. Then terms for z^k. Let's rewrite in standard form: Q(z) = 1 + q1 z + q2 z^2 + ... + q_{L} z^L, where L = max(m+1, 1?) Actually highest degree: from sum_{d=1}^m c[d] z^{d+1}, highest is d=m => z^{m+1}. So L = m+1. So Q(z) = 1 + q1 z + q2 z^2 + ... + q_{m+1} z^{m+1}. But note there may be missing coefficients. Let's compute coefficients:
For k=1: contributions: -z from the -z term; also from -sum c[d] z^d for d=1 gives -c[1] z; from +sum c[d] z^{d+1} for d=0? none. So q1 = -1 - c[1]? Actually check: Q(z) = 1 - z - sum_{d=1}^m c[d] z^d + sum_{d=1}^m c[d] z^{d+1}. So coefficient for z^1: from -z gives -1; from -c[1] z^1 gives -c[1]; from +? No term with d=0 gives +? So q1 = -1 - c[1].
For k from 2 to m: contributions: from -c[k] z^k (since d=k term from -sum) and from +c[k-1] z^k (since d=k-1 gives z^{(k-1)+1}=z^k). So q_k = -c[k] + c[k-1] (for 2 <= k <= m). Note: also maybe -z only contributes to k=1. So q_k = c[k-1] - c[k] for k=2..m.
For k = m+1: contributions: from +c[m] z^{m+1} (since d=m gives z^{m+1}) and no - term because -sum goes only up to z^m. So q_{m+1} = c[m].
Also there might be constant term 1.
But wait we need to check sign: The recurrence from Q(z) G(z) = 1 implies that G(z) satisfies linear recurrence: for n >= L, sum_{i=0}^{L} q_i G_{n-i} = 0? Actually if Q(z) = sum_{i=0}^L q_i z^i, with q_0 = 1, then we have: G_n + q_1 G_{n-1} + ... + q_L G_{n-L} = 0 for n >= L? Let's recall: For generating functions, if Q(z) * G(z) = 1, then the coefficients satisfy convolution: (q * g)n = 1{n=0}. That is, for n=0: q_0 g_0 = 1 => g_0 = 1/q_0 = 1. For n>=1: sum_{i=0}^L q_i g_{n-i} = 0. So recurrence: g_n = - sum_{i=1}^L q_i g_{n-i}. Because q_0=1. So the recurrence is: g_n = -q_1 g_{n-1} - q_2 g_{n-2} - ... - q_L g_{n-L}. So we have coefficients a_i = -q_i.
Thus for S(t) = g_t, we have recurrence:
For t >= L = m+1,
S(t) = -q_1 S(t-1) - q_2 S(t-2) - ... - q_{m+1} S(t-(m+1))
where q_i as derived.
Now compute a_i = -q_i:
For i=1: a_1 = -q_1 = -(-1 - c[1]) = 1 + c[1]. (Matches earlier derived coefficient for S(t-1))
For i=2..m: a_i = -q_i = -(c[i-1] - c[i]) = c[i] - c[i-1]. (Matches earlier derived for S(t-i))
For i=m+1: a_{m+1} = -q_{m+1} = -c[m]. (Matches earlier: -c[m] S(t-(m+1))? Wait earlier we had -c[m] S(t-(m+1)) as a term (negative). But here a_{m+1} = -c[m]. So S(t) includes term a_{m+1} S(t-(m+1)) = -c[m] S(t-(m+1)). That matches earlier derived recurrence (which had -c[m] S(t-(m+1))). Yes.
So recurrence: for t >= m+1,
S(t) = (1 + c[1]) S(t-1) + sum_{i=2}^{m} (c[i] - c[i-1]) S(t-i) - c[m] S(t-(m+1)).
That is order L = m+1. Good.
We also need initial values S(0), S(1), ..., S(m) to use the recurrence. We can compute these using DP (by iterating f(t) up to m, which is at most 100). Since m <=100, that's trivial. So we can compute f(t) for t up to max_d? Actually we need S up to m (which is max_d). Since m=100, we can compute f(t) for t=0..(m+something) using DP up to maybe x if x small but for x <= m we just output S(x) directly from DP. For x > m, we use linear recurrence with exponentiation.
So overall plan:
Read n, x, list of d_i.
Compute c[1..100] array (counts of each distance).
Let m = max distance (max index with c>0) or we can set m=100 always for simplicity, but recurrence order will be up to that m. But careful: If the maximum d with non-zero count is D, then the recurrence for f(t) is effectively order D. But we defined c[d] for d up to 100, many may be zero. The recurrence for S(t) will be order D+1. But we could take m = D (max d). But easier: use m = 100. However that may unnecessarily increase order to 101 even if D < 100, but D <= 100, so max order is 101 anyway. That's fine.
But we can compute m = max d such that c[d] > 0. Then recurrence order L = m+1. However if m = 0? But n>=1, d_i>=1, so m>=1. So L >=2. So we can compute using m.
Now compute f(t) for t=0..L (maybe up to L-1? We need S(0..L-1) as initial states for recurrence? Actually recurrence of order L requires initial values S(0), S(1), ..., S(L-1). Then for t >= L, S(t) = sum_{i=1}^{L} a_i S(t-i). where a_i are coefficients. So we need S(0..L-1). We can compute f(t) for t=0..L-1 (or up to L maybe) by DP. Since L <= 101, we can compute f(t) using DP for t up to maybe L+something? But to compute S(L) itself we could use recurrence but we need initial values. So we compute f(t) for t=0..L-1 and also f(L) maybe if needed? Actually we need S(0..L-1) as initial states. For recurrence, we will compute S(t) for t >= L using fast method. So we can compute f(t) for t=0..L-1 (or up to L) by DP, then compute S(t) as cumulative sum. So we have initial vector S[0..L-1].
Then we want S(x). If x < L, we just output S[x] directly.
If x >= L, we compute S(x) via linear recurrence. We can use Kitamasa algorithm to compute nth term of linear recurrence in O(L^2 log x). Since L <= 101, that's efficient.
So implement Kitamasa:
Given recurrence coefficients a[1..L] (where term S(n) = sum_{i=1}^{L} a[i] * S(n-i) for n >= L), and initial values S[0..L-1]. Compute S(N) mod MOD.
Kitamasa works by computing the polynomial that corresponds to x^N mod characteristic polynomial. We can implement using doubling method:
Define polynomial mod Q(x) = x^L - a1 x^{L-1} - a2 x^{L-2} - ... - aL. The goal is to compute coefficient vector c of x^N expressed as linear combination of {1, x, ..., x^{L-1}} modulo Q. Then S(N) = sum_{i=0}^{L-1} c_i * S(i). Kitamasa can compute c by exponentiation by squaring, starting from x^0 = 1, and for each bit of N, we square the polynomial and reduce modulo Q. The polynomial multiplication (convolution) and reduction are O(L^2). There's also method to multiply two polynomials of degree < L and reduce: since degree after multiplication < 2L, reduction uses recurrence to express higher powers in terms of lower ones. Complexity O(L^2). So overall O(L^2 log N).
We'll need to implement modular arithmetic. Mod = 1e9+7.
Thus steps:
Input n, x.
Read list d of length n.
Compute count array cnt[1..100] (0-index with size 101). For each d_i, cnt[d_i] += 1.
Determine m = max d where cnt[d] > 0. Let L = m+1 (order of recurrence for S).
Compute f[0..L] using DP: f[0] = 1. For t in 1..L (maybe up to L? We'll compute up to L-1? Actually we need S up to L-1, which requires f up to L-1. But for recurrence, we might need initial S[0..L-1]. That's enough. However for computing recurrence coefficients a_i, we need them anyway. We can compute f up to maybe L+m? Not needed. For computing S(L) itself, we might need it if x >= L, but we will compute via Kitamasa, which uses initial S[0..L-1] only. So we only need S[0..L-1]. So we need f[t] for t <= L-1 to compute S[L-1] = sum_{i=0}^{L-1} f[i]. Since L-1 = m. So we need f up to m. But m is at most 100. So compute f[0..m] (m inclusive) but we need S up to L-1 = m. So f[0..m] needed to compute S[m]. However note: f[m] may be needed to compute S[m]? S[m] includes f[m]. So we need f[m] as well. So compute f up to t=m (i.e., L-1). That's fine.
DP: f[t] = sum_{d=1}^{100} cnt[d] * f[t-d] if t-d >=0 else 0. Since m <=100, we can compute f[t] for t=0..m using simple loops: for t from 1 to m: f[t] = sum_{d=1}^{100} cnt[d] * (f[t-d] if t-d>=0 else 0) % MOD. This is O(m * 100) = up to 10k, trivial.
Compute S[t] for t=0..m: S[0] = f[0] = 1; for t=1..m: S[t] = (S[t-1] + f[t]) % MOD. So we have initial S[0..L-1] (since L-1 = m). So S_initial = array of length L, indices 0..L-1.
Compute recurrence coefficients a[1..L] for S:
a_1 = (1 + cnt[1]) % MOD.
For i=2..m: a_i = (cnt[i] - cnt[i-1]) % MOD (might be negative, adjust mod).
a_{m+1} = (-cnt[m]) % MOD. (i.e., a_L = -cnt[m] mod MOD).
Note: cnt[i] is count for distance i; for i > m, cnt[i]=0 (but m is max, so for i>m, cnt[i] = 0. For i = L = m+1, we have a_{m+1} = -cnt[m] mod MOD (since cnt[m+1] = 0, but from formula: a_{m+1} = -cnt[m] (because only from q_{m+1} = cnt[m]). Yes.
But wait, is the recurrence valid even if some cnt[d] are zero? Yes, the derived recurrence holds generally as long as we define m as max d with non-zero? Actually if the maximum d with non-zero count is D, then we can set m = D. For d > D, cnt[d] = 0. Then the recurrence for f(t) involves only d up to D. So the derived recurrence for S(t) yields order D+1. So using m = D is correct. If we used a larger m, say 100, but cnt[d]=0 for d> D, then the recurrence formula still works? Let's check: For d beyond D, cnt[d] = 0, so the coefficients a_i for i up to 101 would involve cnt[i] etc. But note that if we artificially set m to 100, but the actual max non-zero distance is D < 100, then the recurrence derived using m=100 would still be correct? Let's test: Suppose D=2, n=1, d=2. Then cnt[2]=1, cnt[1]=0, cnt[3..100]=0. Then actual f recurrence: f(t)=f(t-2). So order 2. For S, generating function: P(z)=z^2. Then Q(z) = (1-z)(1-z^2)=1 - z - z^2 + z^3. So recurrence for S: S(t) = S(t-1) + S(t-2) - S(t-3)? Actually coefficients: q1 = -1 - c1? c1=0 so q1 = -1; q2 = c1 - c2 = 0-1 = -1? Wait compute using formula: Q(z) = 1 - z - P(z) + z P(z) = 1 - z - z^2 + z^3. So q1 = -1, q2 = -1? Actually we need to express as 1 + q1 z + q2 z^2 + q3 z^3. So q1 = -1, q2 = -1, q3 = 1. Then recurrence: S(t) = -q1 S(t-1) - q2 S(t-2) - q3 S(t-3) = 1 S(t-1) + 1 S(t-2) - 1 S(t-3). That matches: S(t) = S(t-1) + S(t-2) - S(t-3). So order 3 = D+1. If we artificially set m=100 (i.e., include zero coefficients for d>2), then our formulas: a1 = 1 + cnt[1] = 1+0=1; for i=2..100: a_i = cnt[i] - cnt[i-1]. For i=2: cnt[2]-cnt[1]=1-0=1; for i=3: cnt[3]-cnt[2]=0-1=-1; for i=4: cnt[4]-cnt[3]=0-0=0, etc; and a_101 = -cnt[100] = 0. So then the recurrence would be S(t) = a1 S(t-1) + a2 S(t-2) + a3 S(t-3) + ... + a101 S(t-101). But since a4..a101 are zero, effectively S(t) = S(t-1) + S(t-2) - S(t-3). So it's still valid. So we can just take m=100 and L=101, and it will be correct (with many zero coefficients). That simplifies code: we can fix m=100, L=101. Because d_i <= 100, so max distance is at most 100. So we can always set m=100. Then recurrence order L = 101. So we can predefine m = 100. Then we compute cnt[1..100] (size 101 indexing). Then compute f[0..100] using DP (since L-1 = 100). Then compute S[0..100] = cumulative sum. Then compute coefficients a[1..101] using formulas:
a1 = 1 + cnt[1]
For i in 2..100: a_i = cnt[i] - cnt[i-1]
a101 = -cnt[100]
But is it correct for the case when the actual maximum is less than 100? For example, as above D=2, cnt[3..100] are zero, and a3 = cnt[3] - cnt[2] = -1, a4 = 0, ..., a101 = -cnt[100] = 0. So recurrence is S(t) = a1 S(t-1) + a2 S(t-2) + a3 S(t-3). That's correct (order 3). However, the recurrence we implement will be of order 101 but many trailing coefficients are zero. That's fine for Kitamasa: the characteristic polynomial is x^101 - a1 x^100 - ... - a101. But if a101=0, the polynomial is divisible by x? Not necessarily; but it's okay. The polynomial degree is effectively lower, but we can still treat it as degree 101. The Kitamasa algorithm will handle zero coefficients; the reduction step uses the recurrence to reduce degree. Since many high-order coefficients are zero, the reduction may be simpler but still works. Complexity O(L^2) with L=101, still fine.
But we must ensure that the recurrence holds for t >= L (i.e., 101). Does it hold? Derived from generating function, the recurrence holds for all t >= L as long as we include all terms. Since coefficients for higher lags beyond actual max distance might be zero, the recurrence still holds because they are zero. However, we need to verify that for t between actual D+1 and 101, the recurrence might not hold? The recurrence derived from Q(z) * G(z) = 1 holds for all n >= 1? Actually from convolution: for n >= 1, sum_{i=0}^{L} q_i g_{n-i} = 0. This holds for n >= 1? Let's re-derive: If Q(z) G(z) = 1, then for n >= 1, the coefficient of z^n in product is 0. That gives: sum_{i=0}^{L} q_i g_{n-i} = 0, with convention g_{k}=0 for k<0? But for n < L, some indices g_{n-i} with n-i < 0 may appear. Usually we consider generating functions with g_k = 0 for k<0? In our case, S(t) is defined for t>=0 only, and for negative indices we treat as 0. The identity Q(z) G(z) = 1 should hold as formal power series with g_n = S(n) for n>=0 and g_n = 0 for n<0. Then the convolution for n >= 1: sum_{i=0}^{L} q_i g_{n-i} = 0, where g_{n-i}=0 if n-i < 0. So this gives a recurrence that may involve terms with indices less than 0 for small n. But for n >= L, all g_{n-i} have n-i >= 0 because i <= L, so n-i >= n-L >=0. So for n >= L, the recurrence holds purely in terms of non-negative indices. For n < L, the recurrence involves some g with negative indices (which are zero), so it's not a pure recurrence on S only. So indeed the recurrence S(n) = -sum_{i=1}^{L} q_i S(n-i) is valid for n >= L. So for n from L onward, it's correct.
Thus if we take L = 101, the recurrence holds for t >= 101. However, what if actual needed order is less (like D+1=3) and we set L=101, then for t in [3,100] the recurrence might not hold because we are using L=101 but for those t, the recurrence of order 101 would reference S(t-101) which may be negative (t-101 <0) for t<101. So the recurrence S(t) = sum_{i=1}^{101} a_i S(t-i) is only valid for t >= 101, not for t between D+1 and 100. But that's okay because we will only use it for t >= L (i.e., t >= 101). For x < 101, we can just output S[x] directly from DP (since we computed up to 100). For x >= 101, we will apply Kitamasa to compute S(x) using recurrence of order 101, which is valid for t >= 101. So we need to ensure that for t = 101, the recurrence holds: is t=101 >= L? Yes, L=101, so t=101 is exactly the first index where recurrence is valid? t >= L => t >=101. So t=101 is valid. So we can use recurrence for all t >= 101. So our approach: compute S[0..100] via DP, and then for x >= 101, compute S(x) via linear recurrence with order 101 using Kitamasa. Works fine.
But we must be careful: The recurrence derived with L=101 might be valid for t >= 101 only if the polynomial Q(z) degree is exactly 101. But does our definition of Q(z) with cnt[d] for d up to 100 yield polynomial of degree 101? Even if cnt[100]=0, then a101 = -cnt[100] = 0, so the recurrence coefficient for lag 101 is zero, so recurrence effectively of lower order. However, does the recurrence hold for t=101? For t=101, the recurrence would be S(101) = sum_{i=1}^{101} a_i S(101-i). Since a101=0, it effectively uses up to lag 100. But is this equation valid for t=101? It should be, if the underlying generating function Q(z) G(z)=1 with Q(z)=1 + q1 z + ... + q101 z^101 (with q101 possibly 0). Then convolution for n=101 gives: sum_{i=0}^{101} q_i g_{101-i}=0. For i=101, q101 * g0. If q101 = -a101? Actually q101 = -a101? Since a_i = -q_i, so if a101=0 then q101=0, so no issue. So the recurrence S(101) = sum_{i=1}^{101} a_i S(101-i) holds because a101=0, but is it automatically derived from the generating function? For n=101, the convolution gives g101 + q1 g100 + ... + q101 g0 = 0 => g101 = -q1 g100 - ... - q101 g0 = a1 g100 + ... + a101 g0. Since a101 = -q101. So if q101=0, a101=0, then g101 = a1 g100 + ... + a100 g1. So the recurrence holds with order up to 100. But wait, the recurrence we use is with a101 possibly zero. That's fine; it's still valid for n=101 because the term with a101 S(0) would be zero anyway. However, we need to ensure that for n=101, the recurrence holds with the given coefficients. The derivation from generating function holds for any n >= 1? Let's check the boundary: For n = 101, all g_{101-i} for i up to 101 have indices from 0 to 101, all non-negative? g0 is defined. So yes, the convolution includes g0. So for n >= 1, the equation is sum_{i=0}^{L} q_i g_{n-i}=0, provided we treat g_{k}=0 for k<0. For n=101, n-i is >=0 for i<=101, so we include g0. So the recurrence is valid. For n < L, some g_{n-i} with n-i <0 are zero, so the equation becomes something else, but not a pure recurrence. So for n >= L, it's pure. Since L=101, n=101 is >= L, so it's valid.
Thus using L=101 is safe.
But what about the scenario where the actual max distance D is less than 100, and we computed f up to 100 via DP: That's fine because DP uses cnt up to 100. For t > D but <=100, we compute f(t) using recurrence f(t) = sum cnt[d] f(t-d). Since cnt[d]=0 for d > D, f(t) will be computed correctly because only d <= D contribute. So we get correct f(t) up to 100.
Thus we can fix L=101. This simplifies code; we don't need to compute actual m.
Now we need to compute a_i for i=1..101. Let's compute them as described.
But we must verify the formulas with an example to ensure correctness.
Example: Input: n=3, x=3, d=[1,2,3]. Then cnt[1]=1, cnt[2]=1, cnt[3]=1, others 0. m=3? but we set L=101. Then a1 = 1 + cnt[1] = 2. a2 = cnt[2] - cnt[1] = 1 - 1 = 0. a3 = cnt[3] - cnt[2] = 1 - 1 = 0. a4 = cnt[4] - cnt[3] = 0 - 1 = -1? Wait for i=4, cnt[4]=0, cnt[3]=1 => a4 = -1. But our recurrence is up to 101, but we have a4 = -1. But earlier we thought order should be D+1 = 4. Let's compute using generating function to see coefficients. P(z) = z + z^2 + z^3. Q(z) = (1-z)(1 - z - z^2 - z^3) = (1-z)(1 - z - z^2 - z^3) = 1 - z - (z+z^2+z^3) + z*(z+z^2+z^3) = 1 - z - z - z^2 - z^3 + z^2 + z^3 + z^4? Let's compute stepwise:
(1 - z) * (1 - z - z^2 - z^3) = (1)(1 - z - z^2 - z^3) - z(1 - z - z^2 - z^3) = (1 - z - z^2 - z^3) - (z - z^2 - z^3 - z^4) = 1 - z - z^2 - z^3 - z + z^2 + z^3 + z^4 = 1 - 2z + 0z^2 + 0z^3 + z^4. So Q(z) = 1 - 2z + z^4. Then coefficients: q1 = -2, q2=0, q3=0, q4=1. Then recurrence for S: S(t) = -q1 S(t-1) - q2 S(t-2) - q3 S(t-3) - q4 S(t-4) = 2 S(t-1) + 0 + 0 - 1 S(t-4). So S(t) = 2 S(t-1) - S(t-4). That's order 4. So a1 = 2, a2 = 0, a3 = 0, a4 = -1. That matches our computed a1=2, a2=0, a3=0, a4 = -1 (since cnt[4] - cnt[3] = 0-1 = -1). And a5.. a101 would be: a5 = cnt[5] - cnt[4] = 0-0=0, etc, a101 = -cnt[100]=0. So indeed the recurrence is S(t) = 2 S(t-1) - S(t-4) (plus higher-order terms with zero coefficients). So effectively order 4, but we have explicit zero coefficients for lags 2,3,5..101. So our formula yields a4 = -1, correct. So the recurrence is valid for t >= L = 101? But note: the actual recurrence needed to compute S(t) for t>=4 is S(t) = 2 S(t-1) - S(t-4). That's of order 4. However, if we treat L=101, we are saying the recurrence holds for t >= 101, but actually it holds for t >= 4. But we only plan to use it for t >= 101, which is fine because it also holds for t >= 101. But we might miss the chance to use a lower order recurrence for smaller x? But since we have DP for x < 101, we don't need recurrence for x between 4 and 100. So it's okay.
But wait, if x is, say, 150, which is >=101, we can use recurrence with L=101. Since the recurrence is valid for t >= 4, it is certainly valid for t >= 101, so we can compute S(150) using the recurrence with order 101, even though many coefficients are zero. This will work because the linear recurrence is the same for all t >= 101? Let's verify: The recurrence derived from Q(z) G(z)=1 is: for n >= 1, sum_{i=0}^{101} q_i g_{n-i}=0. For n >= 101, all terms involve non-negative indices, so it's valid. For n >= 101, the same equation holds. So using the coefficients a_i = -q_i, the recurrence g_n = sum_{i=1}^{101} a_i g_{n-i} holds for n >= 101. So it's consistent.
Thus we can implement Kitamasa with L=101.
Now we need to compute initial S[0..100] via DP. Let's double-check DP for f up to 100. We have f[0] = 1. For t from 1 to 100:
f[t] = sum_{d=1}^{100} cnt[d] * f[t-d] if t-d >=0 else 0. Since t <= 100, t-d may be negative for d > t, those terms are zero. So we can compute: for d=1..t: if cnt[d]!=0: f[t] += cnt[d] * f[t-d]. For d>t, f[t-d] is zero, we can skip. So O(100*100)=10k.
Then S[t] = (S[t-1] + f[t]) % MOD.
Now Kitamasa:
We have recurrence: for n >= L, S(n) = a1 * S(n-1) + a2 * S(n-2) + ... + aL * S(n-L), where L=101.
Given initial values S[0..L-1] (0-indexed). We want S(N) for N = x (maybe up to 1e9). If N < L, we return S[N] already computed.
We implement Kitamasa (also known as linear recurrence using polynomial exponentiation). Here's a typical algorithm:
Let poly represent coefficients of x^n expressed in basis {1, x, x^2, ..., x^{L-1}} modulo characteristic polynomial.
We compute the polynomial representation of x^N mod charPoly.
Initialize result polynomial res = [1] (representing x^0) and base = [0,1] (representing x^1). Actually we can do exponentiation on the polynomial corresponding to x. But standard approach:
We define function combine(poly1, poly2) that returns polynomial representing (poly1 * poly2) mod charPoly. Since charPoly is: x^L = a1 x^{L-1} + a2 x^{L-2} + ... + aL. So we can reduce a polynomial of degree < 2L to degree < L using the recurrence.
We can then exponentiate by binary exponentiation of N on the polynomial x.
Pseudo:
def kitamasa(N, a, init):
L = len(a) # a[0..L-1] corresponds to a1..aL? We'll use 0-index for convenience.
if N < L: return init[N]
# compute polynomial p = x^N mod Q
# initialize poly for x: [0,1] (coeffs of degree 0 and 1)
poly = [0, 1] # coefficients for degrees 0..L-1? Actually we represent polynomial of degree < L.
# We'll store as list of length L, index i -> coeff for x^i.
# So base polynomial for x is: [0,1,0,0,...0] (size L).
base = [0]*L
base[1] = 1
result = [0]*L
result[0] = 1 # represents 1 (x^0)
while N > 0:
if N & 1:
result = multiply(result, base, a) # result = (result * base) mod Q
base = multiply(base, base, a)
N >>= 1
# Now result polynomial coeffs c[0..L-1] such that x^N = sum_{i=0}^{L-1} c[i] x^i (mod Q)
# Then S(N) = sum_{i=0}^{L-1} c[i] * S(i) mod MOD.
ans = 0
for i in range(L):
ans = (ans + result[i] * init[i]) % MOD
return ans
Define multiply(p, q, a):
# p and q are polynomials of degree < L, representing two remainders.
# Compute r = p * q, then reduce modulo charPoly.
# First compute full product t of size up to 2L-2.
L = len(a)
t = [0](2L-1) # indices 0..2L-2
for i in range(L):
if p[i]==0: continue
for j in range(L):
if q[j]==0: continue
t[i+j] = (t[i+j] + p[i] * q[j]) % MOD
# Now reduce t using recurrence: for k from 2L-2 down to L, replace t[k] contribution:
# since x^L = a1 x^{L-1} + a2 x^{L-2} + ... + aL x^0, we can reduce term t[k] by adding t[k] * coeffs to lower degrees.
# Actually we have polynomial t. For i from 2L-2 down to L:
# coef = t[i]
# t[i] = 0
# for j in 1..L: t[i - j] += coef * a_j? Wait: Since x^i = x^{i-L} * x^L = x^{i-L} * (a1 x^{L-1} + ... + aL). So we add coef * a1 to coefficient of x^{i-1}, coef * a2 to x^{i-2}, ..., coef * aL to x^{i-L}.
# But careful: i - L + (L-1) = i-1? Let's derive: x^{i} = x^{i-L} * x^L. Since x^L = sum_{j=1}^{L} a_j x^{L-j}. Multiply: x^{i-L} * sum_{j=1}^{L} a_j x^{L-j} = sum_{j=1}^{L} a_j x^{i-j}. So indeed we add coef * a_j to term of degree i-j.
# So reduction: for i from 2L-2 down to L:
# coef = t[i]
# for j in 1..L:
# t[i - j] = (t[i - j] + coef * a_j) % MOD
# t[i] = 0
# After this, t[0..L-1] is result.
# Since a_j may be negative? But we treat a_j as coefficient in recurrence S(n) = sum a_j S(n-j). That is the a_j we computed earlier. They may be negative mod MOD. So we should store a_j mod MOD (with values between 0 and MOD-1). So we can do modular arithmetic.
# Implementation: a list a of length L, where a[0] corresponds to a1? We'll use 0-index for convenience: let a[0] = a1, a[1] = a2, ..., a[L-1] = aL.
# Then reduction:
for i in range(2*L-2, L-1, -1): # i from 2L-2 down to L
coef = t[i]
if coef == 0: continue
for j in range(1, L+1):
t[i - j] = (t[i - j] + coef * a[j-1]) % MOD
# t[i] is overwritten, but we don't need it.
# return t[0:L]
return t[:L]
But careful: This reduction loop uses a_j for j up to L, but when i - j may become negative for j > i? But since i >= L, and j up to L, i - j >= i-L >= 0? Actually for i = L, i - L = 0, so i - j for j = L gives 0, okay. For i > L, i - L > 0, so i - j >= i-L >= 1? Actually minimum is i-L when j = L, so i-L >= 0. So indices are non-negative. So safe.
Complexity: multiply O(L^2) due to convolution (L^2) plus reduction O(L^2) because for each i we loop over L. In worst-case, L=101, so each multiply about 2*101^2 ≈ 20k operations, times log2(1e9) ~30 => 600k. Very fast.
Edge Cases: x=0. Then we need count nodes with distance <=0, only root. So answer=1. Our DP: f[0]=1, S[0]=1. For x=0 < L, return S[0]=1. Good.
Check sample: n=3, x=3, d=[1,2,3]. Compute DP up to 100? Actually we only need up to 3 to answer since x=3<101. So DP: f[0]=1; f[1]= cnt[1]f[0] =11=1; f[2]= cnt[1]f[1] + cnt[2]f[0] =11 + 11=2; f[3]= cnt[1]f[2] + cnt[2]f[1] + cnt[3]f[0] =12+11+11=4; S[0]=1; S[1]=2; S[2]=4; S[3]=8. So answer 8. matches.
Another test: n=1, x=1000000000, d=[1]. Then tree is a simple chain, each edge length 1. So number of nodes at distance <= x is x+1. Answer mod MOD = (1e9+1) mod 1e9+7 = 1e9+1? Actually 1e9+1 mod 1e9+7 = 2? Wait 1,000,000,001 - 1,000,000,007 = -6? Actually 1,000,000,001 < 1,000,000,007? 1e9+7 = 1000000007. 1000000001 < 1000000007, so answer = 1000000001. Let's test our method. cnt[1]=1, others 0. Then a1 = 1 + cnt[1] = 2. a2 = cnt[2]-cnt[1] = -1? Actually cnt[2]=0, cnt[1]=1 => a2 = -1. a3 = cnt[3]-cnt[2] = 0-0=0, a4 = cnt[4]-cnt[3]=0-0=0, ... a101 = -cnt[100]=0. So recurrence for S: S(t) = 2 S(t-1) - S(t-2). For chain, S(t)=t+1. Does this recurrence hold? Check: 2(t) - (t-1) = 2t - t +1 = t+1. Yes. So we can compute S(1e9) via Kitamasa. It should work.
Now need to implement Kitamasa correctly.
Potential pitfalls: The recurrence coefficients a_i may be negative. We must mod them into [0, MOD-1] before using in multiplication. So after computing a_i as described, we mod them.
But note: In the recurrence for S(t), the coefficients a_i are as derived. However, we must be careful about the sign for a_{L} = -cnt[100] mod MOD. That's fine.
Now we also need to ensure that initial values S[0..L-1] are correctly computed for the recurrence with these a_i. For t >= L, recurrence holds. But we need to verify that for t = L (101), the recurrence formula with our computed a_i yields the same as DP-computed S[101]? Since we haven't computed S[101] via DP, but we can test with small m to ensure correctness. For the chain example, L=101 but actual order is 2. For t=101, recurrence S(101)=2*S(100)-S(99). Since we have S(100) and S(99) from DP? We compute S up to 100 only. But we need to compute S(101) via recurrence using initial S(0..100). That would be consistent because recurrence holds for all t>=2, and we have S(100) and S(99) computed via DP (which are correct). So for t=101, it should produce correct value. So if we run Kitamasa for large N, it will produce correct answer.
Edge case: x might be exactly 0? Already handled.
Now we need to be careful about using L=101 even if some cnt[d] beyond actual max, but DP for f up to 100 uses cnt up to 100. For DP, if we compute f[t] for t up to 100, we need to ensure we have correct f values. For t near 100, the recurrence f(t) = sum_{d} cnt[d] f(t-d) uses cnt[d] for d up to 100. Since we have all cnt, it's fine.
But there is a nuance: The DP for f[t] should use the recurrence f(t) = sum_{d=1}^{100} cnt[d] f(t-d) if t-d >= 0. That's what we'll do.
Now the Kitamasa algorithm implementation details:
We'll store a list a of length L (101), where a[i] corresponds to a_{i+1} (coefficient for S(n-(i+1))). In the reduction step, we need a_j for j from 1..L. We'll have a array 0-index: a[0]=a1, a[1]=a2,..., a[L-1]=aL.
We'll write multiply(p, q, a):
L = len(a)
compute product t up to 2L-2
t = [0](2L-1)
for i in range(L):
pi = p[i]
if pi==0: continue
for j in range(L):
if q[j]==0: continue
t[i+j] = (t[i+j] + pi * q[j]) % MOD
reduce
for i in range(2*L-2, L-1, -1):
coef = t[i]
if coef == 0: continue
# for j in 1..L: t[i - j] += coef * a[j-1]
# Loop over j from 1 to L
# Equivalent to for idx in range(L):
# t[i - (idx+1)] = (t[i - (idx+1)] + coef * a[idx]) % MOD
for idx in range(L):
t[i - idx - 1] = (t[i - idx - 1] + coef * a[idx]) % MOD
# t[i] will be ignored, we can set to 0 but not necessary as we slice.
return t[:L]
Potential optimization: precompute a list. This is fine.
Now exponentiate: We'll compute polynomial representation of x^N. We'll use binary exponentiation on the polynomial representing x (base). We'll store result polynomial initially as [1] + zeros (size L). For each bit of N, if bit set, result = multiply(result, base, a). Then base = multiply(base, base, a). After loop, result is poly for x^N.
Then compute ans = sum_{i=0}^{L-1} result[i] * init[i] % MOD.
Test on chain example with small x to verify.
But we must consider that N can be huge (1e9). The algorithm O(L^2 log N) is okay.
Now we should confirm that S(x) computed via this method equals the sum of f(i). We should test with some random small parameters by brute force to ensure correctness.
Let's implement a quick mental test: n=2, d=[1,2]. cnt[1]=1, cnt[2]=1. We'll compute manually small x.
Now our recurrence for S: Let's compute coefficients: cnt[1]=1, cnt[2]=1, others 0. Then a1 = 1 + cnt1 =2. a2 = cnt2 - cnt1 = 0. a3 = cnt3 - cnt2 = 0-1 = -1. So recurrence: S(t) = 2 S(t-1) - S(t-3). Check: For t=3, S3 = f0+f1+f2+f3 = 1+1+2+3=7. Using recurrence: 2S2 - S0 = 24 -1 =7, ok. For t=4, S4 = S3+f4=7+5=12. Recurrence: 2S3 - S1 = 27 -2 =12, ok. So order 3.
Now L=101, but effective order is 3. Our method should compute S(x) for any x.
For x=5 (<101) we would DP: S5 = S4 + f5 =12+8=20. Using recurrence: 2S4 - S2 = 212 -4 =20. Works.
Now if we try to compute S(10) using Kitamasa with L=101, we need to have initial S[0..100] computed correctly. We compute S up to 100 via DP? That's feasible. But wait, to compute S up to 100 via DP we need to compute f up to 100. For chain example, f(t) grows exponentially but we only need modulo. That's fine. So we can compute S[0..100] via DP using recurrence f(t) = f(t-1)+f(t-2) (since cnt[1]=cnt[2]=1). That's easy.
Now Kitamasa for N=10 with L=101, but we have initial S[0..100]. Since N<101, we would not go into the exponentiation; we would return S[10] directly. So to test for N>=101, we need a case where x >100. Example: n=1,d=[1], x=1000. Then S(x)=x+1=1001. Our DP computes S up to 100 only, but we need to compute S(1000) using Kitamasa. Let's test with code logic: We compute S[0..100] via DP (which for chain is i+1). So S[100]=101. Then using Kitamasa with recurrence S(t)=2S(t-1)-S(t-2) (order 2) but we use L=101 with many zeros. Should compute S(1000) = 1001 mod MOD. That's correct if algorithm works.
Now we must ensure that the Kitamasa algorithm correctly handles zero coefficients and that the polynomial representation of x^N mod charPoly is correct even when charPoly is not monic? But we assume it's monic: x^L - a1 x^{L-1} - ... - aL = 0, so charPoly is monic. That's correct.
Implementation details for multiply: The reduction step uses a_j from the recurrence: x^L = a1 x^{L-1} + a2 x^{L-2} + ... + aL. So when reducing term x^i for i>=L, we replace x^i = a1 x^{i-1} + a2 x^{i-2} + ... + aL x^{i-L}. So we add coef * a_j to coefficient of i-j. That's what we do.
Now potential pitfalls: The multiplication may cause t[i] to be updated many times, and we iterate i from high to low, and we also modify t[i-j] which may be >= L and thus we might later process those indices when i is higher? But we process i from high to low, so when we reduce a term at index i, we add contributions to lower indices (i-j). Since i-j < i, and we already processed higher i, we may later process i-j if i-j >= L? Actually i-j could be >= L for some j? For i = 2L-2, j=1 gives i-1 = 2L-3 which is >= L? Possibly if L>1, 2L-3 >= L for L>3? For L=101, 2L-3=199 >=101, so yes, we might be adding to indices that are still >= L. But we are iterating i from high down to L, so when we reduce i, we might increase some t[k] for k < i but possibly still >= L. Since we haven't processed those k yet (because we are going downwards), those increased values will be processed later when we reach i = k. That's correct and necessary. So the order is important: from highest to L. So it's correct.
Now after processing all i, t[0..L-1] holds the reduced polynomial.
Now verify that we don't exceed list bounds: t length is 2L-1, indices 0..2L-2. For i from 2L-2 down to L, i-j ranges from i-L down to i-1? For j= L, i-L >= 0. So i-L >= 0 always. And i-1 <= 2L-3, still within array. So okay.
Now the multiplication function must return a list of length L.
Now exponentiation: We'll need to handle N as integer up to 1e9. Use while N>0.
Now one more nuance: The Kitamasa algorithm usually uses initial terms init[0..L-1] as S(0)..S(L-1). However, our recurrence is valid for n >= L. So when we compute S(N) for N >= L, it's correct. But what about N exactly L? That's fine.
Now implement:
def solve():
read n, x
d list
MOD = 1000000007
cnt = [0]101 # index 1..100
for val in d:
cnt[val] += 1
L = 101
# compute f up to 100
f = [0](L) # indices 0..100
f[0] = 1
for t in range(1, L):
total = 0
# only d from 1 to 100, but we can loop d=1..100 and if t-d>=0
# faster: for d in range(1, min(t,100)+1)
# but since 100 is small, we can just loop all d
for d in range(1, 101):
if t >= d:
total = (total + cnt[d] * f[t-d]) % MOD
f[t] = total
# compute S up to 100
S = [0]*L
S[0] = f[0]
for t in range(1, L):
S[t] = (S[t-1] + f[t]) % MOD
if x < L:
print(S[x] % MOD)
return
# compute recurrence coefficients a of length L
a = [0]*L
# a1 = 1 + cnt[1]
a[0] = (1 + cnt[1]) % MOD
for i in range(2, 101): # i from 2 to 100 => corresponds to a_i
# a_i = cnt[i] - cnt[i-1]
a[i-1] = (cnt[i] - cnt[i-1]) % MOD # i-1 because a index 0-> a1
# a_101 = -cnt[100]
a[100] = (-cnt[100]) % MOD # index 100 = a101
# ensure positivity
for i in range(L):
a[i] %= MOD
code
# Kitamasa
def multiply(p, q):
# p, q length L
# compute product r = p*q mod charPoly
t = [0]*(2*L - 1)
for i in range(L):
if p[i]==0:
continue
pi = p[i]
for j in range(L):
if q[j]==0:
continue
t[i+j] = (t[i+j] + pi * q[j]) % MOD
# reduction
for i in range(2*L-2, L-1, -1):
coeff = t[i]
if coeff == 0:
continue
# add coeff * a_j to t[i-j] for j=1..L
# for idx in range(L): t[i-idx-1] += coeff * a[idx]
# loop unroll maybe
for idx in range(L):
t[i-idx-1] = (t[i-idx-1] + coeff * a[idx]) % MOD
return t[:L]
# compute polynomial for x^N
N = x
# base polynomial: x
base = [0]*L
base[1] = 1
res = [0]*L
res[0] = 1
while N > 0:
if N & 1:
res = multiply(res, base)
base = multiply(base, base)
N >>= 1
# compute answer
ans = 0
for i in range(L):
ans = (ans + res[i] * S[i]) % MOD
print(ans)
Test with sample.
But there may be off-by-one in the recurrence coefficients. Let's test with simple chain manually using code logic (or we can simulate mentally for small N beyond L? But for chain, L=101, but we can test with a smaller L for verification. However, we trust the derivation.
But to be safe, we should test with some random small n, small x that are >100 but we can brute force using DP up to x? But x up to 1e9 but we can test with small x like 200 and compare with DP up to 200 (since we can compute f up to 200 easily) to ensure Kitamasa works. But we can't run now but we can reason.
Potential issue: The recurrence derived using a_i from cnt might assume that the generating function for S includes denominator (1-z)(1-P(z)). But we used Q(z) = (1-z)(1-P(z)). However, we must ensure that the constant term q0 = 1, which is true. So the recurrence g_n = -q1 g_{n-1} - ... - q_L g_{n-L} for n >= L is correct. And we defined a_i = -q_i. So a_i = -q_i.
Now from our a_i formulas, we have:
a1 = 1 + cnt[1] corresponds to -q1? Let's verify:
From earlier: q1 = -1 - cnt[1]. So -q1 = 1 + cnt[1] => a1. Good.
For i in 2..100: q_i = cnt[i-1] - cnt[i]? Wait we derived q_i = c[i-1] - c[i] for 2<=i<=100? Let's re-derive: For Q(z) = 1 - z - sum_{d=1}^{100} cnt[d] z^d + sum_{d=1}^{100} cnt[d] z^{d+1}. Write it as:
Now Kitamasa expects recurrence S(n) = sum_{i=1}^{L} a_i S(n-i) for n >= L. That's what we have.
Now we need initial S[0..L-1] which we computed via DP. That's fine.
But one subtle point: The recurrence with L=101 may require S values for indices n-i that might be negative for n near L? But for n >= L, n-i >= 0 for i <= L, because n-L >=0. So okay.
Now for x >= L, we compute S(x) via Kitamasa. This should work.
But we must ensure that our DP computed S[0..100] correctly using the recurrence f(t) = sum cnt[d] f(t-d). That is correct.
Now test with chain: n=1, d=[1]. Compute cnt[1]=1. DP: f[0]=1; f[1]=cnt[1]*f[0]=1; f[2]=cnt[1]*f[1]=1; etc. So f[t]=1 for all t? Wait, if only one child with distance 1, then the tree is like each node has one child edge length 1. The number of nodes at distance exactly t: that's exactly 1 (the unique node at depth t). Because there's exactly one path of length t (all edges 1). So f(t)=1 for all t. So S(t)=t+1. So DP: f[0]=1; f[1]=cnt[1]*f[0] =1; f[2]= cnt[1]*f[1] =1; etc. So correct.
Now compute a: a1 = 1+1=2; a2 = cnt[2]-cnt[1] = 0-1 = -1; a3..a100 = (cnt[i]-cnt[i-1]) = 0-0=0; a101 = -cnt[100] = 0. So recurrence: S(t)=2S(t-1) - S(t-2). For S(t)=t+1, we have 2(t) - (t-1) = 2t - t +1 = t+1. Works.
Now Kitamasa for N=1000 should give 1001.
Now about modulo: a2 = -1 mod MOD = MOD-1. That's fine.
Now check the multiplication reduction: When we reduce, we use a array values mod MOD. Negative values become positive after mod.
Now we must ensure that we treat a indices correctly: In reduction, for each i we add coeff * a[idx] to t[i-idx-1]. This uses a[0] for j=1, a[1] for j=2,... a[L-1] for j=L. That's correct.
Now test multiply for simple case where L=2 maybe. But we can trust.
Now we also need to compute f[t] for t up to 100 efficiently. Implementation note: Since cnt has up to 100 entries, we can compute f[t] by iterating over d where cnt[d] > 0, but we can just loop d=1..100; it's cheap.
Now potential pitfalls: The DP for f[t] might have large values mod MOD, but fine.
Now check sample: x=3, less than 101, we output S[3]=8. Good.
Now test with x=100 for chain: S[100]=101. For x=101, we would use Kitamasa. Let's simulate quickly to see if Kitamasa yields 102. Since x=101 >=101, we compute. The initial S array: S[0]=1, S[1]=2,... S[100]=101. The recurrence S(101)=2S(100)-S(99)=2101-100=102. Kitamasa should compute correctly.
Now confirm that Kitamasa with L=101 and these a_i will compute S(101) correctly.
Let's do a mental run for N=101: we compute polynomial for x^101 mod charPoly. For such a recurrence, the polynomial representation of x^N corresponds to the coefficients that express S(N) in terms of initial S. Actually Kitamasa basically does exponentiation in the companion matrix via polynomial; it's correct.
Now one more check: The base polynomial for x is [0,1,0,...,0]. Multiply result = [1] (x^0). When N=101, we compute res = base^101. This is essentially the vector that corresponds to the state transition. It's known to work.
Thus the solution should be correct.
But we should also consider the possibility that x is 0 and n up to 1e5, d_i up to 100. Works.
Now we should also test with n=100000, but all d_i random; we compute cnt. Since cnt size 100, it's fine.
Now potential issue: The DP for f[t] up to 100 uses recurrence f(t) = sum cnt[d] f(t-d). However, we must be careful: the recurrence for f(t) holds for all t >=1. For t up to 100, that's fine.
Now what about the case where some distances are large, e.g., d_i=100. Then f(t) for t<100 may be zero except when t is multiple? That's fine.
Now we need to ensure that the DP initial f[0]=1 is correct. f(0) corresponds to empty path (root). Yes.
Now final answer = S[x] modulo MOD.
Now consider alternative: Could there be an issue with the recurrence for S when x is large and cnt[100] is zero? Then a_101 = 0, so order effectively less. But Kitamasa still works.
Now also need to consider the possibility that x is huge, like 10^9, and L=101, so exponentiation log2(x) ~ 30 steps. That's fine.
Now we should verify the memory: small.
Now one more nuance: The recurrence we derived for S uses coefficients a_i that might have negative values. In Kitamasa multiplication reduction, we add coeff * a[idx] which could be negative if we haven't modded? We mod a[idx] to [0, MOD-1] so it's positive. So fine.
Now the DP for f[t] and S[t] also mod MOD.
Now we should test with random small n and small x via brute force enumerating all sequences? Not needed.
Now think about potential off-by-one: L=101 corresponds to indices up to 100. But our DP computed f up to 100 and S up to 100. So initial vector length 101 (indices 0..100). But the recurrence order is 101, so we need initial values S(0) through S(100). That's exactly what we have. Good.
Now if x = 100, we output S[100] directly, not using recurrence. Good.
Now we need to ensure that for x > 100, the recurrence is valid. As argued, it's valid for n >= 101. So for x=101, it's valid. For x=1000, also.
Now we must also consider that the generating function approach yields recurrence that holds for n >= L. But is there any chance that the recurrence doesn't hold for n = L if some q_i for i where n-i becomes negative? For n = L, all terms S(n-i) have indices n-i = L-i, ranging from L-1 down to 0. So all non-negative. So it's fine.
Now we might want to test the derived recurrence with an example where x is exactly 101. For chain: S(101) should be 102. Our DP only gives up to 100. Using Kitamasa with a recurrence S(n) = 2S(n-1)-S(n-2) (plus many zero terms) will compute correctly. Let's quickly simulate Kitamasa for N=101 with L=101? Actually the recurrence we have is effectively order 2, but we embed it in order 101. That means the characteristic polynomial is x^101 - a1 x^100 - a2 x^99 - ... - a101. Since a3..a100 are zero, a101=0, it's x^101 - 2 x^100 + x^99? Actually a1=2, a2=-1, others 0, so polynomial = x^101 - 2 x^100 + x^99 = x^99 (x^2 - 2x +1) = x^99 (x-1)^2. So the polynomial has factors x^99 and (x-1)^2. The minimal polynomial is (x-1)^2, but we are using larger polynomial. Kitamasa will still work because any polynomial that vanishes on the sequence (i.e., satisfies the linear recurrence) can be used as modulus. Since the sequence satisfies the larger recurrence, it's fine.
Thus our approach should work.
Now we should implement carefully with modular arithmetic and avoid overhead.
Potential optimization: In multiply, we can skip loops when coefficients zero. Already we check p[i]==0 and q[j]==0 continue. Also in reduction, we check coeff !=0.
But we can also precompute for each i the contributions? Not necessary.
Now we should also consider that MOD is prime but not needed.
Now edge case: n may be large, but d_i up to 100, so cnt may sum to n.
Now we need to read input: The first line contains n and x. The next line contains n integers.
Now handle the case x may be 0. In DP, S[0]=1. That's fine.
Now one more check: For x extremely large (1e9) and mod 1e9+7, no issue.
Now I'll code the solution in Python.
But wait: There is a nuance: In the DP for f[t], we used f[t] = sum_{d} cnt[d] * f[t-d]. That is correct for number of sequences (ordered). However, does this count each node exactly once? Yes, each node corresponds to a unique sequence of child choices, so indeed number of nodes at distance exactly t is the number of sequences summing to t. So f[t] is correct.
Now I'm comfortable.
Now before finalizing, test with another sample from the problem? They only provided one.
Let's test some small cases manually with brute force code mentally:
Case 1: n=1, d=2, x=3. Tree: root, child at distance 2, then its child at distance 4? Actually distances: root to child (2). That child's child at distance 2, so total 4. So nodes within distance 3: only root and the child at distance 2? Actually root (0), child (2). That's 2 nodes. So answer 2. Let's compute: cnt[2]=1. f0=1, f1=0 (since no d=1), f2=cnt2f0=1, f3=cnt2f1=0. So S3 = f0+f1+f2+f3 = 1+0+1+0=2. DP: f up to 100: fine. Our algorithm: x=3<101, DP gives S[3]=2. OK.
Case 2: n=2, d=[1,1] (two children both length 1). Then tree is a complete binary tree with each edge length 1. Number of nodes at distance exactly t is 2^t. So total nodes up to distance x is sum_{i=0}^x 2^i = 2^{x+1} - 1. For x=3, answer=15. Let's compute with our method: cnt[1]=2. f0=1, f1=2f0=2, f2=2f1=4, f3=2f2=8, S3=1+2+4+8=15. Good. Recurrence for S: a1=1+cnt1=3, a2=cnt2-cnt1=0-2=-2? Wait cnt[2]=0, cnt[1]=2 => a2 = -2 mod? Actually a2 = cnt[2]-cnt[1] = -2. So recurrence S(t)=3S(t-1)-2S(t-2). Check: S0=1, S1=3? Actually S1= f0+f1=3. Recurrence for t=2: 3S1 - 2S0 = 9-2=7, but S2 should be 1+2+4=7. Good. t=3: 37 -2*3 =21-6=15. Works. So correct.
Now test x=5, S5=2^6-1=63. Recurrence: 331 -215 =93-30=63? Wait S4=1+2+4+8+16=31, S3=15, so 331-215=93-30=63. Good.
Now for x=1000, our method should compute 2^{1001}-1 mod MOD, which can be computed directly as well, but Kitamasa should match.
Thus algorithm seems solid.
Now implement.
Potential pitfalls: In DP for f[t], we use modulo. But we must compute f[t] exactly modulo MOD. Since recurrence uses addition and multiplication, it's fine.
Now I realize: The DP for f[t] uses cnt[d] which can be up to n (1e5). So cnt[d] * f[t-d] might be up to 1e5 * MOD, but we do modulo after multiplication; in Python, multiplication of ints is fine.
Now we also need to compute S up to 100. S[100] may be huge but modded.
Now we must ensure that we treat a_i as mod MOD before using in multiply. Already we do a[i] %= MOD.
Now a detail: When we compute a[0] = (1 + cnt[1]) % MOD, but cnt[1] may be > MOD? Actually cnt[1] <= n <= 1e5 < MOD, so fine. But still we mod for safety.
Now implement.
One more: For the multiply function, we might want to use local variables to avoid global lookups. We'll define inside solve, using closure for a and L.
Now test runtime: L=101, log2(1e9) ~ 30 steps, each multiply does up to L^2 = 10k loops for product and L^2 for reduction? Actually product double loops over L each: at most 101101=10,201 operations, but many may be skipped if zeros. However, in worst case, all cnt may be non-zero? cnt up to 100 but each cnt could be >0, making many f[t] non-zero, so base and result polynomials may become dense. But even if all coefficients become non-zero, that's 10k multiplications and 10k reductions (since reduction loops over i from 2L-2 down to L, which is about L-1 ~100, and inside loop over L=101, so about 10k operations). So each multiply about 20k ops. 30 steps (since we have up to 30 multiplications for base squaring and some for result) but actually each iteration we do at most 2 multiplies: one for squaring base, and possibly one for multiply result. So total multiplies ~ 230 = 60. So 60 * 20k = 1.2 million operations. Very fast in Python.
Thus the solution is efficient.
Now we need to ensure correct handling of x = 0.
Now I'll write the code accordingly.
But before finalizing, I want to double-check the DP for f[t] when t up to 100. The recurrence f[t] = sum_{d=1}^{100} cnt[d] * f[t-d] (with f[negative]=0). That is correct. However, we must consider that for t up to 100, the recurrence may involve f[t-d] with t-d negative which we treat as 0. So implementing as loop for d in range(1, t+1) is fine; but using range(1,101) and condition if t>=d is also fine.
Now for initial S[0] = f[0] = 1.
Now about L: I set L=101. But maybe we could compute m = max distance with non-zero cnt to reduce L, but not necessary.
But there is a subtle point: The recurrence for S we derived using L=101 may not hold for n = 101 if cnt[100] is non-zero? Actually it holds. But check: In the derivation we used max d = 100; if the maximum distance is less than 100, some q_i beyond actual max are zero. So recurrence still holds.
Now final code.
Edge Cases:
n = 100000, each d_i = 100. Then cnt[100] = 100000. Others 0. f[0]=1; f[t] for t<100: 0 (since no d <= t? Actually f[t] for t from 1 to 99: sum cnt[d] f[t-d] = 0 because t-d <0 for d=100? Actually if t<100, then the only d that could contribute is d <= t, but none because cnt[d] non-zero only for d=100 and 100 > t, so f[t]=0. f[100] = cnt[100] * f[0] = 100000. f[101] = cnt[100] * f[1] = 0; f[102] = cnt[100] * f[2] =0; etc? Actually f[200] = cnt[100]f[100] = 100000 * 100000 = 1e10 mod? So pattern: only multiples of 100 have non-zero f. So S(t) counts nodes at distances multiples of 100. Our DP up to 100: f[0]=1, f[100]=100000, others 0. So S[100]=1+100000? Actually S[100] = f[0] + f[1]+...+f[100] = 1 + 099 + 100000 = 100001. For x=100, answer = 100001. For x=200, would be 1 + 100000 + (100000^2?) Actually f[200] = cnt[100]f[100] = 100000100000 = 10^10, so S(200)= 1 + 100000 + 10^10. So we can compute via recurrence. Our method should handle this.
Now verify recurrence for S for this case: cnt[100]=K (K=100000). Others 0. Then a1 = 1 + cnt[1] = 1+0=1. For i=2..100: a_i = cnt[i] - cnt[i-1]. Since only cnt[100]=K, for i=100: a_100 = cnt[100] - cnt[99] = K - 0 = K. For i=101: a_101 = -cnt[100] = -K. Also for i=2..99, a_i = 0. Also a1=1. So recurrence: S(t) = 1S(t-1) + 0 + ... + 0 + KS(t-100) - KS(t-101). That is S(t) = S(t-1) + KS(t-100) - KS(t-101). Does that hold for S(t) = sum_{i} f(i)? Let's test small: For t=100, recurrence would be S(100) = S(99) + KS(0) - KS(-1). Since S(-1)=0, S(99) = 1 (only root), so S(100) = 1 + K1 = 1+K, correct. For t=101: S(101) = S(100) + KS(1) - KS(0) = (1+K) + K1? Wait S(1)=1? Actually f[1]=0 => S(1)=1. So S(101)= (1+K) + K1 - K1 = 1+K. So S(101)=1+K (since f[101]=0). That matches: S(101)=f0+f100 = 1+K. Good. For t=200: S(200) = S(199) + KS(100) - K*S(99). S(199) =? But anyway it's correct. So recurrence works.
Thus our a_i calculation seems correct.
Now we should consider if any overflow issues in Python? Not with mod.
Now implement.
One more detail: In Kitamasa, we need to compute polynomial for x^N. For N=0, we would have res = [1,0,...]. But we already handle N >= L, and N = x, which could be 0. But we have early return for x<L, so for x=0, we return S[0]=1. So we don't call Kitamasa with N=0. But if L were smaller and x >= L but x=0 can't be >= L (since L>=2). So fine.
Now code.
Let's write in Python 3.
Implementation steps:
def solve():
import sys
MOD = 1000000007
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
x = int(next(it))
d_vals = [int(next(it)) for _ in range(n)]
code
cnt = [0]*101
for d in d_vals:
cnt[d] += 1
L = 101 # order for S recurrence
f = [0]*(L)
f[0] = 1
for t in range(1, L):
total = 0
# For d from 1 to 100, if t>=d
# We can limit loop to min(t,100)
upper = min(t, 100)
for d in range(1, upper+1):
if cnt[d]:
total = (total + cnt[d] * f[t-d]) % MOD
f[t] = total
S = [0]*L
S[0] = f[0]
for t in range(1, L):
S[t] = (S[t-1] + f[t]) % MOD
if x < L:
print(S[x] % MOD)
return
# compute a coefficients
a = [0]*L
a[0] = (1 + cnt[1]) % MOD
for i in range(2, 101): # i = 2..100
a[i-1] = (cnt[i] - cnt[i-1]) % MOD
a[100] = (-cnt[100]) % MOD
# ensure mod
for i in range(L):
a[i] %= MOD
# Kitamasa
def mul(p, q):
# p and q are length L lists
# compute product modulo characteristic polynomial
t = [0]*(2*L - 1)
# convolution
for i in range(L):
pi = p[i]
if pi == 0:
continue
for j in range(L):
qj = q[j]
if qj == 0:
continue
t[i+j] = (t[i+j] + pi * qj) % MOD
# reduction
for i in range(2*L-2, L-1, -1):
coeff = t[i]
if coeff == 0:
continue
# add coeff * a_k to t[i-k-1] for k from 0 to L-1
# corresponding to a_{k+1}
# we can do loop
for k in range(L):
t[i - k - 1] = (t[i - k - 1] + coeff * a[k]) % MOD
return t[:L]
# compute x^N
N = x
base = [0]*L
base[1] = 1
res = [0]*L
res[0] = 1
while N > 0:
if N & 1:
res = mul(res, base)
base = mul(base, base)
N >>= 1
ans = 0
for i in range(L):
ans = (ans + res[i] * S[i]) % MOD
print(ans)
Now we need to ensure the code runs quickly. The DP loop for f up to 100 may be a bit heavy due to inner loop over d up to 100 for each t, but 10k is trivial.
Now we should consider whether we need to use iterative DP for f up to 100 with modulo. Already done.
Now we might also compute f and S up to L-1 only; we used L=101, so up to 100. That's good.
Now test chain n=1,d=1,x=1000. We'll simulate quickly (can't run but trust). We would compute f up to 100: all ones. S up to 100: S[t]=t+1. Then a: a0=2, a1=-1 mod, others 0. Kitamasa: compute res for N=1000. Should output 1001 mod MOD=1001.
Now potential bug: In the reduction loop, we use t[i - k - 1]. For i up to 2L-2 = 200, and k up to 100, i - k - 1 minimal is when i=101, k=100 => 101-100-1=0, ok. For i=200, k=0 =>199, within array.
Now one more nuance: In reduction, we might be adding to t[i - k - 1] which could be >= L and may be processed later if i - k - 1 >= L. Since we are iterating i from high to low, that's fine: when we later reach i = that index (which is less than current i), we will process it. However, we also need to ensure we don't accidentally process the same coefficient multiple times in a way that causes over-reduction? The standard approach is to reduce from high to low exactly once per i, and after processing all i, the polynomial degree < L. This works because we treat each term as we go, and subsequent reductions will handle higher-degree contributions added from previous reductions. Because we go from highest index downwards, any new contribution added to an index that is still >= L will be processed later (since its index is lower than current i, but we haven't processed it yet because we are moving downwards; but we will encounter it when i reaches that index). This is correct.
One thing: When we add coeff * a[k] to t[i - k - 1], we are modifying t at index lower than i. If i - k - 1 is still >= L, then later when we reach that index in the outer loop (i decreases), we will process it. However, we must be careful that we do not process the same term multiple times? It's fine because we only process each index once as we pass through. However, if we add to an index that is less than the current i but greater than or equal to L, that index will be processed later when the outer loop reaches that i value. But if we had already processed that index earlier (since we go from high to low), but we haven't because we are currently at a higher i and we haven't visited that lower index yet (since we go downwards). So it's safe.
Now after reduction, we slice t[:L], discarding high indices. This yields polynomial of degree < L.
Now one more optimization: Instead of building t of length 2L-1 each time, we could allocate once and reuse, but not needed.
Now potential issue: For large n, cnt[d] could be up to 1e5, but f[t-d] mod MOD, multiplication cnt[d] * f[t-d] could be up to 1e5 * MOD ~ 1e14, Python int handles it.
Now I think the solution is ready.
But wait: There is a nuance: The DP for f[t] uses cnt[d] for d up to 100. But is the recurrence f(t) = sum cnt[d] f(t-d) valid for t > 0? Yes. However, we must confirm that the root is counted only once. f(0)=1 counts the root. For t>0, any node at distance t is reached by a path ending with some edge of length d, and the number of ways to reach the parent node is f(t-d). Since each parent node has exactly cnt[d] children at distance d, the number of nodes at distance t is sum_d cnt[d] * f(t-d). So it's correct.
Now all good.
Now I will provide the final answer with explanation.
But before final, let's consider if there is any scenario where x is exactly 100 and we use DP; okay.
Now we should also ensure that we handle modulo negative values properly. For a[i] we did % MOD after possibly negative, but Python's % yields non-negative.
Now final code.
But perhaps we can further simplify by using matrix exponentiation of size 101. However Kitamasa is simpler to code and more efficient.
Now I'll produce final answer.
I'll also add some comments in code for clarity.
Now one more test: n=2, d=[1,2], x=100. Compute using DP? Since x=100 <101, we compute S[100] via DP. That's fine. But DP up to 100: we need to compute f[t] for t up to 100. The recurrence f[t] = f[t-1] + f[t-2] (since cnt1=1,cnt2=1). So that's Fibonacci. We can compute mod. Should be fine.
Now test for x=1000000000, same recurrence. Kitamasa should compute quickly.
Now one more nuance: The DP for f up to 100 may require careful indexing when t-d negative; we used condition t>=d. Good.
Now final.
But I've just thought: The recurrence for S we derived uses a[0] = 1 + cnt[1] etc. However, this assumes that we used L=101. But is there any chance that the recurrence fails for t when some cnt[d] are zero? Already considered.
Now I think it's correct.
Now produce final solution explanation and code.
But wait: In the problem statement, it says: "each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to di." This implies that the children are ordered: i-th child has distance d_i. So our cnt approach counts each child type separately. Yes.
Now final answer.
Now I'll provide the final answer. ```python
import sys
MOD = 1_000_000_007
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
x = int(next(it))
d_vals = [int(next(it)) for _ in range(n)]
code
# count how many children have each length (1..100)
cnt = [0] * 101
for d in d_vals:
cnt[d] += 1
L = 101 # order of the linear recurrence for S(t)
# ----- DP for f(t) and S(t) up to t = 100 -----
f = [0] * L
f[0] = 1 # empty path → root
for t in range(1, L):
total = 0
up = min(t, 100)
for d in range(1, up + 1):
if cnt[d]:
total = (total + cnt[d] * f[t - d]) % MOD
f[t] = total
S = [0] * L
S[0] = f[0]
for t in range(1, L):
S[t] = (S[t - 1] + f[t]) % MOD
if x < L: # answer directly from precomputed table
print(S[x] % MOD)
return
# ----- coefficients of the linear recurrence for S(t) -----
a = [0] * L # a[i] corresponds to coefficient for S(t-(i+1))
a[0] = (1 + cnt[1]) % MOD # a1 = 1 + c1
for i in range(2, 101): # i = 2 .. 100 -> a_i = c_i - c_{i-1}
a[i - 1] = (cnt[i] - cnt[i - 1]) % MOD
a[100] = (-cnt[100]) % MOD # a_{101} = -c_{100}
for i in range(L):
a[i] %= MOD
# ----- Kitamasa: compute S(x) using the recurrence -----
def multiply(p, q):
"""return (p * q) modulo the characteristic polynomial x^L - a1 x^{L-1} - ... - aL"""
# full product (degree < 2L-1)
t = [0] * (2 * L - 1)
for i in range(L):
pi = p[i]
if pi == 0:
continue
for j in range(L):
qj = q[j]
if qj == 0:
continue
t[i + j] = (t[i + j] + pi * qj) % MOD
# reduce high degrees using x^L = a1 x^{L-1} + ... + aL
for i in range(2 * L - 2, L - 1, -1):
coeff = t[i]
if coeff == 0:
continue
# add coeff * a_k to position i - k - 1 (k = 0..L-1 corresponds to a_{k+1})
for k in range(L):
t[i - k - 1] = (t[i - k - 1] + coeff * a[k]) % MOD
return t[:L]
N = x
base = [0] * L
base[1] = 1 # polynomial representing x
res = [0] * L
res[0] = 1 # polynomial representing 1
while N:
if N & 1:
res = multiply(res, base)
base = multiply(base, base)
N >>= 1
ans = 0
for i in range(L):
ans = (ans + res[i] * S[i]) % MOD
print(ans)