#B3901. Rearrange Sequence Under Adjacent Sum Constraints
Rearrange Sequence Under Adjacent Sum Constraints
Rearrange Sequence Under Adjacent Sum Constraints
Given a sequence \(a\) of length \(n\) where each \(a_i\) is an integer power of 2, and an integer \(x\), determine the number of permutations \(p\) (i.e. rearrangements) of the indices \(1,2,\dots,n\) such that if we define a new sequence \(a'\) by \(a'_i = a_{p_i}\), then for every valid index \(i\) (\(1 \le i < n\)) the sum of two adjacent elements satisfies
[ a'i + a'{i+1} \le x. ]
Two rearrangements are considered different if the permutation \(p\) is different. Since the answer can be large, output it modulo \(10^9+7\).
Note: Although the numbers in the sequence may be equal, they are considered as distinct items (i.e. permutations that only differ by the positions of equal numbers are counted separately).
inputFormat
The first line contains two integers \(n\) and \(x\) separated by a space.
The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) separated by spaces. It is guaranteed that each \(a_i\) is a power of 2.
outputFormat
Output a single integer denoting the number of valid rearrangements of \(a\) modulo \(10^9+7\).
sample
3 3
1 2 1
6