using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("1 3 2");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("3");
WillReturn.Add("1 3 3");
//2
}
else if (InputPattern == "Input3") {
WillReturn.Add("4");
WillReturn.Add("2 2 2 2");
//0
}
else if (InputPattern == "Input4") {
WillReturn.Add("6");
WillReturn.Add("83 65 79 54 88 69");
//44
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
const long Hou = 1000000000 + 7;
static void Main()
{
List<string> InputList = GetInputList();
long[] BArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
Dictionary<long, long> ZaatuDict = DeriveZaatuDict(BArr);
// 場合の数[最後の値]
var NextUp_Fenwick_Tree = new Fenwick_Tree(ZaatuDict.Count - 1, Hou);
var NextDown_Fenwick_Tree = new Fenwick_Tree(ZaatuDict.Count - 1, Hou);
long UB = NextUp_Fenwick_Tree.GetUB();
long Answer = 0;
foreach (long EachB in BArr) {
// 増加させる場合
long RangeSta1 = 0;
long RangeEnd1 = ZaatuDict[EachB] - 1;
long RangeCnt1 = NextUp_Fenwick_Tree.GetSum(RangeSta1, RangeEnd1);
NextDown_Fenwick_Tree.Add(ZaatuDict[EachB], RangeCnt1);
Answer += RangeCnt1;
Answer %= Hou;
// 減少させる場合
long RangeSta2 = ZaatuDict[EachB] + 1;
long RangeEnd2 = UB;
long RangeCnt2 = NextDown_Fenwick_Tree.GetSum(RangeSta2, RangeEnd2);
NextUp_Fenwick_Tree.Add(ZaatuDict[EachB], RangeCnt2);
Answer += RangeCnt2;
Answer %= Hou;
// ここから始める場合
NextDown_Fenwick_Tree.Add(ZaatuDict[EachB], 1);
NextUp_Fenwick_Tree.Add(ZaatuDict[EachB], 1);
}
Console.WriteLine(Answer);
}
//////////////////////////////////////////////////////////////////////////
// 列挙を引数として、座標圧縮し、座圧後の値[座圧前の値]なDictを返す
//////////////////////////////////////////////////////////////////////////
static Dictionary<long, long> DeriveZaatuDict(IEnumerable<long> pEnum)
{
var ZaatuDict = new Dictionary<long, long>();
var ValSet = new HashSet<long>(pEnum);
long No = 0;
foreach (long EachVal in ValSet.OrderBy(pX => pX)) {
ZaatuDict[EachVal] = No;
No++;
}
return ZaatuDict;
}
}
// フェニック木
#region Fenwick_Tree
internal class Fenwick_Tree
{
private long[] mBitArr;
private long mExternalArrUB;
private long mHou;
// ノードのIndexの列挙を返す
internal IEnumerable<long> GetNodeIndEnum()
{
for (long I = 0; I <= mExternalArrUB; I++) {
yield return I;
}
}
// 木のノードのUBを返す
internal long GetUB()
{
return mExternalArrUB;
}
// コンストラクタ
internal Fenwick_Tree(long pExternalArrUB, long pHou)
{
mExternalArrUB = pExternalArrUB;
// フェニック木の外部配列は0オリジンで、
// フェニック木の内部配列は1オリジンなため、2を足す
mBitArr = new long[pExternalArrUB + 2];
mHou = pHou;
}
// [pSta,pEnd] のSumを返す
internal long GetSum(long pSta, long pEnd)
{
long Result = GetSum(pEnd) - GetSum(pSta - 1);
Result %= mHou;
if (Result < 0) Result += mHou;
return Result;
}
// [0,pEnd] のSumを返す
internal long GetSum(long pEnd)
{
pEnd++; // 1オリジンに変更
long Sum = 0;
while (pEnd >= 1) {
Sum += mBitArr[pEnd];
Sum %= mHou;
pEnd -= pEnd & -pEnd;
}
if (Sum < 0) Sum += mHou;
return Sum;
}
// [I] に Xを加算
internal void Add(long pI, long pX)
{
pI++; // 1オリジンに変更
pX %= mHou;
while (pI <= mBitArr.GetUpperBound(0)) {
mBitArr[pI] += pX;
mBitArr[pI] %= mHou;
pI += pI & -pI;
}
}
}
#endregion