You are given an array of integers nums and an integer target.

Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.

&nbsp;
Example 1:

Input: nums = [3,5,6,7], target = 9
Output: 4
Explanation: There are 4 subsequences that satisfy the condition.
[3] -&gt; Min value + max value &lt;= target (3 + 3 &lt;= 9)
[3,5] -&gt; (3 + 5 &lt;= 9)
[3,5,6] -&gt; (3 + 6 &lt;= 9)
[3,6] -&gt; (3 + 6 &lt;= 9)


Example 2:

Input: nums = [3,3,6,8], target = 10
Output: 6
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]


Example 3:

Input: nums = [2,3,3,4,6,7], target = 12
Output: 61
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 - 2 = 61).


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 106
	1 &lt;= target &lt;= 106


class Solution:
    def numSubseq(self, nums: List[int], target: int) -> int:
        nums.sort()
        left, right = 0, len(nums) - 1
        count = 0
        mod = 10 ** 9 + 7
        
        while left <= right:
            if nums[left] + nums[right] > target:
                right -= 1
            else:
                count += pow(2, right - left, mod)
                left += 1
        
        return count % mod
class Solution {
    public int numSubseq(int[] nums, int target) {
        int n = nums.length;
        int i =0, j = n-1;
        int mod = (int)1e9+7;
        Arrays.sort(nums);
        int[] pow = new int[n];
        pow[0]=1;
        int count =0;
        for(int z =1;z<n;z++){
            pow[z] = (pow[z-1]*2)%mod;
        }
        
        while(i<=j){
            if((nums[i]+nums[j]) <= target){
                count=(count+pow[j-i])%mod;
                i++;
            }
            else if((nums[i]+nums[j]) > target)
                j--;
        }
        return count;
    }
}
class Solution {
private:
    int mod=1e9+7;
    int multiply(int a,int b){
        if(b==0){
            return 0;
        } else if(b%2==0){
            int ans=multiply(a,b/2);
            return (ans%mod+ans%mod)%mod;
        } else {
            int ans=multiply(a,b-1);
            return (a%mod+ans%mod)%mod;
        }
    }
    int power(int b,int e){
        if(e==0){
            return 1;
        } else if(e%2==0){
            int ans=power(b,e/2);
            return multiply(ans,ans);
        } else {
            int ans=power(b,e-1);
            return multiply(b,ans);
        }
    }
public:
    int numSubseq(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        int start=0,end=0;
        while(end<nums.size() and nums[start]+nums[end]<=target){
            end++;
        }
        end--;
        int ans=0;
        while(start<=end){
            if(nums[start]+nums[end]<=target){
                ans=(ans%mod+power(2,end-start))%mod;
                start++;
            } else {
                end--;
            }
        }
        return ans;
    }
};
const MOD = 1000000007;

var numSubseq = function(nums, target) {
    nums.sort((a, b) => a - b);
    const len = nums.length;
    const pow = new Array(len).fill(1);
    for(let i = 1; i < len; i++) {
        pow[i] = (pow[i - 1] * 2) % MOD;
    }
    let l = 0, r = len - 1, ans = 0;
    while(l <= r) {
        if(nums[l] + nums[r] > target) {
            r--; continue;
        } else {
            ans = (ans + pow[r - l]) % MOD;
            l++;
        }
    }
    return ans % MOD;
};
Number of Subsequences That Satisfy the Given Sum Condition