fork download
  1. // Shaazzz
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. const int N = 1000006, Mod = 1e9 + 7;
  5. int n, tot, F[N], dp[N];
  6. inline void Add(int i)
  7. {
  8. for (; i < N; i += i & -i)
  9. F[i] ++;
  10. }
  11. inline int Get(int i)
  12. {
  13. int ret = 0;
  14. for (; i; i -= i & -i)
  15. ret += F[i];
  16. return (ret);
  17. }
  18. int main()
  19. {
  20. scanf("%d", &n);
  21. dp[1] = 0;
  22. for (int i = 2; i < N; i++)
  23. dp[i] = (1ll * dp[i - 1] * i + 1ll * (i - 1) * (i + 1) / 2) % Mod;
  24. for (int i = 1; i <= n; i++)
  25. {
  26. int a, c;
  27. scanf("%d", &a);
  28. c = a - 1 - Get(a);
  29. Add(a);
  30. tot = (tot + 1ll * c * (dp[n - i] + (n - i + 2) / 2)) % Mod;
  31. }
  32. return !printf("%d", tot);
  33. }
  34.  
Success #stdin #stdout 0s 23048KB
stdin
Standard input is empty
stdout
Standard output is empty