挺简单的一道组合数学,每个数字a单独看(设位置为p),有 2^(n-1)不同的序列会包含自己,但是要减掉自己前驱是a的情况,设前驱的位置是x,这种情况有 2^(x^1) * 2^(n-p)种排列,对每个数求前驱是a的情况的加和即可
看代码吧,cnt[k] = sigma(2^(x-1)) (a[x] == k)
最后结果是每个数字的值与unique后包含自己的情况总数的乘积和
#pragma warning(disable:4996)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
const int maxn = 100005;
int b[maxn],a[maxn];
int n,m;
LL cnt[maxn];
LL B[maxn];
int main() {
int t;
cin >> t;
B[0] = 1;
for (int i = 1;i < maxn;i++) B[i] = (B[i - 1] * 2) % mod;
while (t--) {
memset(cnt, 0, sizeof(cnt));
cin >> n;
for (int i = 0;i < n;i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
m = unique(b, b + n) - b;
LL ret = 0;
for (int i = 0;i < n;i++) {
int x = lower_bound(b, b + m, a[i]) - b;
ret = ((ret - ((cnt[x] * (LL)a[i]) % mod * B[n - i - 1])%mod)%mod+mod)%mod;
ret = (ret + B[n - 1] * (LL)a[i]) % mod;
cnt[x] = (cnt[x] + B[i])%mod;
}
cout << ret << endl;
}
return 0;
}