using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; using nint=System.Int64; static class Program{ const long inf=long.MaxValue>>1; const int mod=(int)1e9+7; static Sc sc=new Sc(); static void Main(){ Pt pt=new Pt(Solve); pt.Sm(); } static void Solve(Pt pt) { var n=sc.I; var a=sc.Ia; var avl=new Avl(); var la=new long[n]; var ra=new long[n]; long p=1; for(int i = 0;i < n;i++) { avl.Ud(a[i],p); la[i]=avl.Rd(0,a[i]-1); la[i]%=mod; ra[i]=avl.Rd(a[i]+1,inf); ra[i]%=mod; p=(p<<1)%mod; } p=1; avl=new Avl(); var ans=0L; for(int i = n - 1;i >= 0;i--) { ans+=avl.Rd(0,a[i]-1)%mod*la[i]; ans+=avl.Rd(a[i]+1,inf)%mod*ra[i]; ans%=mod; avl.Ud(a[i],p); p=(p<<1)%mod; } pt.Wl(ans); } } public class Avl{ public class Nd{ public int h=1,c=1; public readonly nint n; public long d,ds; public Nd l,r; public Nd(nint n,long d,Nd du){this.n=n;this.d=d;ds=d;l=du;r=du;} } public Nd root,du; public int cnt=0; public Avl(){ du=new Nd(nint.MinValue,0,du);du.h=0;du.c=0; root=du; } public void Ud(nint n,long d){ if(root==du){root=new Nd(n,d,du);cnt++;} else if(root.n==n){root.d+=d;root.ds+=d;} else if(Fu(root,n,d)){ if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);} cnt++; } } private bool Fu(Nd t,nint n,long d){ t.ds+=d; bool bo=false; if(t.n>n){ if(t.l!=du){ if(t.l.n==n){t.l.d+=d;t.l.ds+=d;} else if(bo=Fu(t.l,n,d)){ t.c++; if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);} t.h=Max(t.l.h,t.r.h)+1; } } else{t.l=new Nd(n,d,du);bo=true;t.c++;t.h=Max(t.l.h,t.r.h)+1;} } else{ if(t.r!=du){ if(t.r.n==n){t.r.d+=d;t.r.ds+=d;} else if(bo=Fu(t.r,n,d)){ t.c++; if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);} t.h=Max(t.l.h,t.r.h)+1; } } else{t.r=new Nd(n,d,du);bo=true;t.c++;t.h=Max(t.l.h,t.r.h)+1;} } return bo; } public void Ud2(nint n,long d){ if(root==du){root=new Nd(n,d,du);} else{ Fu2(root,n,d); if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);} } cnt++; } private void Fu2(Nd t,nint n,long d){ t.ds+=d; t.c++; if(t.n>n){ if(t.l!=du){ Fu2(t.l,n,d); if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);} } else{t.l=new Nd(n,d,du);} } else{ if(t.r!=du){ Fu2(t.r,n,d); if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);} } else{t.r=new Nd(n,d,du);} } t.h=Max(t.l.h,t.r.h)+1; } private Nd Rotate(Nd t){ Nd nd=du; if(t.l.h>t.r.h){ if(t.l.l.h>t.l.r.h){ nd=t.l;t.l=t.l.r;nd.r=t; Ft(t); } else{ nd=t.l.r; t.l.r=nd.l;nd.l=t.l; Ft(t.l); t.l=nd.r;nd.r=t; Ft(t); } } else{ if(t.r.l.h>t.r.r.h){ nd=t.r.l; t.r.l=nd.r;nd.r=t.r; Ft(t.r); t.r=nd.l;nd.l=t; Ft(t); } else{ nd=t.r;t.r=t.r.l;nd.l=t; Ft(t); } } Ft(nd); return nd; } private void Ft(Nd t){ t.h=Max(t.l.h,t.r.h)+1; t.c=t.l.c+t.r.c+1; t.ds=t.l.ds+t.r.ds+t.d; } public bool Dl(nint n){ if(cnt==0){return false;} Nd t=root; if(t.n==n){ root=Fd2(root); if(cnt==0){return true;} else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);} else{Ft(root);} return true; } bool bo=Fd1(root,n); if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);} else{Ft(root);} return bo; } private bool Fd1(Nd t,nint n){ if(t.n>n){ if(t.l==du){return false;} else if(t.l.n!=n){ bool bo=Fd1(t.l,n); if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);} else{Ft(t.l);} return bo; } else{ t.l=Fd2(t.l); Ft(t); return true; } } else{ if(t.r==du){return false;} else if(t.r.n!=n){ bool bo=Fd1(t.r,n); if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);} else{Ft(t.r);} return bo; } else{ t.r=Fd2(t.r); Ft(t); return true; } } } private Nd Fd2(Nd dn){ cnt--; if(dn.l==du){return dn.r;} if(dn.r==du){return dn.l;} Nd u=dn.l; if(u.r==du){ u.r=dn.r; Ft(u); return u; } u=Fd3(u,dn); if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);} else{Ft(dn.l);} Ft(u); return u; } private Nd Fd3(Nd u,Nd dn){ if(u.r.r!=du){ Nd v=Fd3(u.r,dn); if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);} else{Ft(u.r);} return v; } else{ Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r; return v; } } public Nd Hs(nint n){ Nd t=root; if(cnt==0){return null;} if(t.n==n){return t;} Nd a=null; while(true){ if(t.n>n){ if(t.l==du){break;} else if(t.l.n!=n){t=t.l;} else{a=t.l;break;} } else{ if(t.r==du){break;} else if(t.r.n!=n){t=t.r;} else{a=t.r;break;} } } return a; } public Nd Ra(int n){return cnt>=n&&cnt!=0?Fr(root,n):null;} private Nd Fr(Nd t,int n){ if(t.l.c>n-1){return Fr(t.l,n);} if(t.l.cn){ if(t.l==du){return t;} Nd u=Flb(t.l,n); return u==null?t:u; } return t; } public Nd Ub(nint n){return cnt!=0?Fub(root,n):null;} private Nd Fub(Nd t,nint n){ if(t.n>n){ if(t.l==du){return null;} return Fub(t.l,n); } if(t.n=a,r);} if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);} return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1; } public long Rd(nint a,nint b){return cnt!=0?Frd(root,a,b,false,false):0;} private long Frd(Nd t,nint a,nint b,bool l,bool r){ if(t.h==0){return 0;} if(l&&r){return t.ds;} if(t.n=a,r);} if(t.n>b){return Frd(t.l,a,b,l,t.n<=b);} return Frd(t.l,a,b,l,t.n<=b)+Frd(t.r,a,b,t.n>=a,r)+t.d; } [Obsolete] public void Ad(int n,int d){ Nd t=root; if(cnt==0){return;} if(t.n==n){t.d+=d;t.ds+=d;return;} while(true){ t.ds+=d; if(t.n>n){ if(t.l==du){break;} else if(t.l.n!=n){t=t.l;} else{t.l.d+=d;t.l.ds+=d;break;} } else{ if(t.r==du){break;} else if(t.r.n!=n){t=t.r;} else{t.r.d+=d;t.r.ds+=d;break;} } } } public void En(Action f){if(cnt>0){Fen(root,f);}} private void Fen(Nd t,Action f){ f(t); if(t.l!=du){Fen(t.l,f);} if(t.r!=du){Fen(t.r,f);} } } public class Pt{ private StringBuilder sb=new StringBuilder(); public Pt(Action f){f(this);} public void W(int s){sb.Append(s);} public void W(long s){sb.Append(s);} public void W(double s){sb.Append(s);} public void W(decimal s){sb.Append(s);} public void W(char s){sb.Append(s);} public void W(string s){sb.Append(s);} public void W(object s){sb.Append(s);} public void W(string f,int s){sb.Append(s.ToString(f));} public void W(string f,long s){sb.Append(s.ToString(f));} public void W(string f,double s){sb.Append(s.ToString(f));} public void W(string f,decimal s){sb.Append(s.ToString(f));} public void Wl(int s){sb.Append(s).Append(Environment.NewLine);} public void Wl(long s){sb.Append(s).Append(Environment.NewLine);} public void Wl(double s){sb.Append(s).Append(Environment.NewLine);} public void Wl(decimal s){sb.Append(s).Append(Environment.NewLine);} public void Wl(char s){sb.Append(s).Append(Environment.NewLine);} public void Wl(string s){sb.Append(s).Append(Environment.NewLine);} public void Wl(object s){sb.Append(s).Append(Environment.NewLine);} public void Wl(string f,int s){sb.Append(s.ToString(f)).Append(Environment.NewLine);} public void Wl(string f,long s){sb.Append(s.ToString(f)).Append(Environment.NewLine);} public void Wl(string f,double s){sb.Append(s.ToString(f)).Append(Environment.NewLine);} public void Wl(string f,decimal s){sb.Append(s.ToString(f)).Append(Environment.NewLine);} public void Wl(int[] s){sb.Append(string.Join(" ",s)).Append(Environment.NewLine);} public void Wl(long[] s){sb.Append(string.Join(" ",s)).Append(Environment.NewLine);} public void Wl(double[] s){sb.Append(string.Join(" ",s)).Append(Environment.NewLine);} public void Wl(string[] s){sb.Append(string.Join(" ",s)).Append(Environment.NewLine);} public void Wl(object[] s){sb.Append(string.Join(" ",s)).Append(Environment.NewLine);} public void Wl(){sb.Append(Environment.NewLine);} public void Sm(){Console.Write(sb);} public static bool operator==(Pt a,Pt b){return a.sb.ToString()==b.sb.ToString();} public static bool operator!=(Pt a,Pt b){return a.sb.ToString()!=b.sb.ToString();} public override bool Equals(object obj){return false;} public override int GetHashCode(){return 0;} } public class Sc{ [MethodImpl(MethodImplOptions.AggressiveInlining)] protected virtual string Rl(){return Console.ReadLine();} [MethodImpl(MethodImplOptions.AggressiveInlining)] protected virtual string[] Sp(string st){return st.Split();} [MethodImpl(MethodImplOptions.AggressiveInlining)] private T Ct(string s){return (T)Convert.ChangeType(s,typeof(T));} public virtual int I{get{return int.Parse(Rl());}} public virtual long L{get{return long.Parse(Rl());}} public virtual double D{get{return double.Parse(Rl());}} public virtual string S{get{return Rl();}} public int[] Ia{get{return Array.ConvertAll(Sp(Rl()),int.Parse);}} public long[] La{get{return Array.ConvertAll(Sp(Rl()),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Sp(Rl()),double.Parse);}} public string[] Sa{get{return Sp(Rl());}} public object[] Oa{get{return Sp(Rl());}} public int[] Ia2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),int.Parse);}} public int[] Ia3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),int.Parse);} public int[] Ia3(int a){return Array.ConvertAll(Sp(Rl()+" "+a.ToString()),int.Parse);} public long[] La2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),long.Parse);}} public long[] La3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),long.Parse);} public long[] La3(int a){return Array.ConvertAll(Sp(Rl()+" "+a.ToString()),long.Parse);} public double[] Da2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),double.Parse);}} public double[] Da3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),double.Parse);} public T[] Arr(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T) Tp3(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T) Tp4(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T,T) Tp5(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]),Ct(s[4]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T,T,T) Tp6(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]),Ct(s[4]),Ct(s[5]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T1,T2) Tp2(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T1,T1,T2) Tp3(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} } public class Scr:Sc{ private List li=new List(); private int l=0; private bool bo=false; [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override string Rl(){ if(bo){return li[l++%li.Count];} li.Add(Console.ReadLine()); return li[li.Count-1]; } public void Again(){bo=true;} } public class Sc2:Sc{ private string[] sps=new string[]{" "," ","\t"}; [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override string[] Sp(string st){return st.Split(sps,StringSplitOptions.RemoveEmptyEntries);} public override int I{get{return int.Parse(Sp(Rl())[0]);}} public override long L{get{return long.Parse(Sp(Rl())[0]);}} public override double D{get{return double.Parse(Sp(Rl())[0]);}} public override string S{get{return Sp(Rl())[0];}} } public class Sct:Sc{ private List li=new List(); private int l=0; public void Add(int s){li.Add(s.ToString());} public void Add(long s){li.Add(s.ToString());} public void Add(double s){li.Add(s.ToString());} public void Add(string s){li.Add(s.ToString());} public void Add(object s){li.Add(s.ToString());} public void Add(int[] s){li.Add(string.Join(" ",s));} public void Add(long[] s){li.Add(string.Join(" ",s));} public void Add(double[] s){li.Add(string.Join(" ",s));} public void Add(string[] s){li.Add(string.Join(" ",s));} public void Add(object[] s){li.Add(string.Join(" ",s));} protected override string Rl(){return li[l++];} public void Clear(){li.Clear();l=0;} public void Again(){l=0;} public void Pf(){Console.WriteLine(string.Join("\n",li));} }