2 6 1 3 2 6 5 4 5 3 5 2 4 1
Case #1: 10 Case #2: 1
//小中大+小大中=sigma (rightmax-1)*rightmax/2=total
//小中大 = sigma leftmin*rightmax
//小大中=total-小中大
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110000;
const int mod=100000007;
int c[maxn];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
int sum(int x)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
long long leftmin[maxn],rightmax[maxn];
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
long long ans=0;
for(int i=1;i<=n;i++)
{
leftmin[i]=sum(a[i]);
update(a[i],1);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
rightmax[i]=sum(n)-sum(a[i]);
update(a[i],1);
}
for(int i=1;i<=n;i++)
{
long long tmp=((rightmax[i]-1)*rightmax[i]/2)%mod;
long long cnt=(leftmin[i]*rightmax[i])%mod;
long long tnow=((tmp-cnt)%mod+mod)%mod;
ans=(ans+tnow)%mod;
}
printf("Case #%d: %I64d\n",pl++,ans);
}
return 0;
}