import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 class BitSum: def __init__(self, n): self.n = n+1 self.table = [0]*self.n self.aa = [0]*n def __getitem__(self, item): return self.aa[item] def add(self, i, x): self.aa[i] += x i += 1 while i < self.n: self.table[i] += x i += i & -i def sum(self, i): i += 1 res = 0 while i > 0: res += self.table[i] i -= i & -i return res def sumlr(self, l, r): if l >= r: return 0 if l == 0: return self.sum(r-1) return self.sum(r-1)-self.sum(l-1) def solve(): res = 0 ia = sorted(enumerate(aa), key=lambda x: x[1]) ll, rr = BitSum(n), BitSum(n) pend = [] for i, a in ia: if pend and pend[0][1] != a: for j, _ in pend: ll.add(j, pow(2, j, md)) j = n-1-j rr.add(j, pow(2, j, md)) pend.clear() res += ll.sum(i)*rr.sum(n-1-i)%md res %= md pend.append((i, a)) return res n = II() aa = LI() ans = solve() aa = [-a for a in aa] ans += solve() print(ans%md)