Author : shailesh_2004
Required Knowledge :Combinatorics, Binary Exponentation, Segment Trees
Time Complexity :
Editorialist : shailesh_2004
Fix the minimum element and try to find the number of subsequences.
Let the fixed minimum element be at index, and is an index right side to such that , let be the number of elements in between and index that are less than , then the total number of subsequences with and elements as extremites are , but we also added a case where the elements of the subsequence are only and , but the size should be atleast , so remove this and add to answer.
Similarly, take as index left side to such that (We are not taking because equal to case is already included in the above), let be the number of elements in between and index that are less than , then the total number of subsequences with and elements as extremites are , but we also added a case where the elements of the subsequence are only and , but the size should be atleast , so remove this and add to answer.
While computing , use binary exponentation.
Note: This can also be solved using Segment Trees.
// Author : Shailesh
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vll vector<ll>
#define pb push_back
#define mp make_pair
#define endl "\n"
#define mod 1000000007
#define tot(a) a.begin(),a.end()
#define minii *min_element
#define maxii *max_element
ll binexp(ll a,ll b,ll mod2=mod)
{
ll res=1;
a%=mod2;
while(b)
{
if(b%2)res=(res*a)%mod2;
a=(a*a)%mod2;
b>>=1;
}
return res;
}
void solve()
{
ll n;
cin>>n;
ll a[n+1],i;
for(i=1;i<=n;i++)cin>>a[i];
ll j,cnt;
ll ans=0;
for(i=1;i<=n;i++)
{
cnt=0;
for(j=i+1;j<=n;j++)
{
if(a[j]>=a[i])
{
ans=(ans+binexp(2,cnt)-1)%mod;
}
if(a[j]<a[i])
{
cnt++;
}
}
cnt=0;
for(j=i-1;j>=1;j--)
{
if(a[j]>a[i])
{
ans=(ans+binexp(2,cnt)-1)%mod;
}
if(a[j]<a[i])
{
cnt++;
}
}
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll t;cin>>t;while(t--)
solve();
return 0;
}