CodeEff/ECCO huggingface dataset:
# Original solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    if a%b == 0:
        print((a+b))
    else:
        print((b-a))
    return

#B
def B():
    n,m = LI()
    d = defaultdict(int)
    for i in range(n):
        l = LI()
        for j in range(1,len(l)):
            d[l[j]] += 1
    ans = 0
    for i in list(d.values()):
        if i == n:
            ans += 1
    print(ans)
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    B()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    ans = [1]*m
    for i in range(n):
        q = LI()
        for j in q[1:]:
            ans[j-1] += 1
        for j in range(m):
            ans[j] -= 1
    res = 0
    for i in ans:
        if i == 1:
            res += 1
    print(res)    
    return

#Solve
if __name__ == "__main__":
    solve()
# Original solution
N,M=list(map(int,input().split()))
A=[list(map(int,input().split()[1:])) for i in range(N)]
food=set(range(1,M+1))
for i in range(N):
    food&=set(A[i])
print((len(food)))

# Optimized solution
N,M=list(map(int,input().split()))
food=set(range(1,M+1))
for i in range(N):
    K,*A=list(map(int,input().split()))
    food&=set(A)
print((len(food)))
# Original solution
n,m = list(map(int,input().split()))
a = [list(map(int,input().split())) for i in range(n)]
ans = 0
cnt = 0

for i in range(1,m+1):
    for j in range(n):
        for k in range(1,a[j][0]+1):
            if a[j][k] == i:
                cnt += 1
                break
    if cnt == n:
        ans += 1
    cnt = 0

print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
a = [list(map(int,input().split())) for i in range(n)]
ans = 0
cnt = 0

for i in range(1,m+1):
    for j in a:
        if i in j[1:]:
            cnt += 1
    if cnt == n:
        ans += 1
    cnt = 0

print(ans)
# Original solution
n,m = list(map(int, input().split()))

l = []
for i in range(n): #Kを除く好きな食べ物を取得
    a = [int(_) for _ in input().split()]
    l.append(a[1:a[0]+1])

y = [] #全部好きな人を要素’1’として格納
for j in range(1,m+1):
    x = 0
    for k in range(n):
        if j in l[k]:
            x += 1
    if x == n:
        y.append(1)
        
print((len(y)))


# Optimized solution
n,m = list(map(int, input().split()))

l = []
for i in range(n): #Kを除く好きな食べ物を取得
    a = [int(_) for _ in input().split()]
    l.append(a[1:a[0]+1])

y = [] #全部好きな人を要素’1’として格納
for j in range(1,m+1):
    x = 0
    for k in range(n):
        if j in l[k]:
            x += 1
    if x == n: #これが全部好きの条件
        y.append(1)
        
print((len(y)))
# Original solution
n,m=list(map(int,input().split()));a=[]
for _ in[0]*n:a.extend(list(map(int,input().split()))[1:])
print((sum(n==a.count(i+1)for i in range(m))))

# Optimized solution
I=lambda:list(map(int,input().split()))
n,m=I()
a=[]
for _ in[0]*n:a.extend(I()[1:])
print((sum(n==a.count(i+1)for i in range(m))))
# Original solution
N,M = list(map(int,input().split()))
list_list = [list(map(int,input().split())) for i in range(N)]

count = 0

for i in range(1,M+1):
  flg = True
  for list in list_list:
    if i in list[1:]:
      continue
    else:
      flg = False
      break
  if flg == True:
  	count += 1
print(count)
      


# Optimized solution
N, M = list(map(int, input().split()))
a = set(list(map(int, input().split()))[1:])
for i in range(N - 1):
    a &= set(list(map(int, input().split()))[1:])
print((len(a)))
# Original solution
N, M = list(map(int, input().split()))
K = [0] * N
A = [[0 for j in range(M)] for i in range(N)] 
for i in range(N):
    inpt = list(map(int, input().split())) 
    K[i] = inpt[0] 
    for j in range(1, K[i]+1, 1): 
        A[i][j-1] = inpt[j]
count = 0
for j in range(1, M+1, 1): 
    likecount = 0
    for i in range(N): 
        for k in range(K[i]): 
            if A[i][k] == j:
                likecount += 1
    if likecount == N: 
        count += 1
print(count)


# Optimized solution
N, M = list(map(int, input().split()))
K = [0] * N
A = [list(map(int, input().split())) for i in range(N)]
count = 0
for i in range(1, M+1, 1): 
    likecount = 0
    for j in range(N): 
        for k in range(1, A[j][0]+1, 1): 
            if A[j][k] == i:
                likecount += 1
    if likecount == N: 
        count += 1
print(count)
# Original solution
N, M = list(map(int, input().split()))
K = [0] * N
A = [list(map(int, input().split())) for i in range(N)]
count = 0
for i in range(1, M+1, 1): 
    likecount = 0
    for j in range(N): 
        for k in range(1, A[j][0]+1, 1): 
            if A[j][k] == i:
                likecount += 1
    if likecount == N: 
        count += 1
print(count)


# Optimized solution
N, M = list(map(int, input().split()))
likeset = set(range(1, M+1)) 
for _ in range(N):
    a, *b = list(map(int, input().split())) 
    likeset &= set(b) 
print((len(likeset)))
# Original solution
from collections import defaultdict
n, m = list(map(int, input().split()))
s = []
for _ in range(n):
    k, *a = list(map(int, input().split()))
    s.append(a)
dd = defaultdict(int)
for l in s:
    for k in l:
        dd[k] += 1

print((sum(1 if v == n else 0 for k, v in list(dd.items()))))


# Optimized solution
n, m = list(map(int, input().split()))
s = set(range(1, m+1))
for _ in range(n):
    k, *a = list(map(int, input().split()))
    s &= set(a)
print((len(s)))
# Original solution
n, m = list(map(int, input().split()))
S = set(range(1, m+1))

for i in range(n):
    k, *a = list(map(int, input().split()))
    S &= set(a)

print((len(S)))


# Optimized solution
n, m = list(map(int, input().split()))

s = set(range(1, m+1))
for i in range(n):
    k, *a = list(map(int, input().split()))
    s &= set(a)

print((len(s)))
# Original solution
n, m = list(map(int, input().split()))
ans = [0] * m
for _ in range(n):
    k, *a = [int(x) for x in input().split()]
    for i in a:
        ans[i-1] += 1
tmp = 0
for a in ans:
    if a == n:
        tmp += 1
print(tmp)


# Optimized solution
n, m = list(map(int, input().split()))
ans = set(map(str, list(range(1, m+1))))
for _ in range(n):
    ans = ans & set(input().split()[1:])
print((len(ans)))
# Original solution
a = list(map(int,input().split()))
cnt = [0,] *(a[1]+1)
for i in range(a[0]):
    suki = list(map(int,input().split()))
    for j in range(1, len(suki)):
        cnt[suki[j]]+=1
ans = 0
for i in range(1, a[1]+1):
    if cnt[i] == a[0]:
        ans+=1
print((str(ans)))

# Optimized solution
import sys
readline = sys.stdin.readline

a = list(map(int,input().split()))
N = a[0]
M = a[1]
cnt = [0,]*(M+1)
ans = 0
for i in range(N):
    K, *like, = readline().split()
    K = int(K)
    like = [int(s) for s in like]
    for j in range(K):
        cnt[like[j]] += 1
for i in range(1, M+1):
    if cnt[i] == N:
        ans += 1
print((str(ans)))
# Original solution
N, M = list(map(int,input().split()))
A = [list(map(int,input().split())) for _ in range(N)]
ans = 0
for i in range(1,M+1):
  cnt = 0
  for a in A:
    if(i in a[1:]):
      cnt += 1
  if(cnt == N):
    ans += 1
print(ans)

# Optimized solution
N, M = list(map(int,input().split()))
A = [list(map(int,input().split())) for _ in range(N)]

tmp = [0]*(M+1)
for a in A:
  for aa in a[1:]:
    tmp[aa] += 1
ans = 0
for t in tmp:
  if t == N:
    ans += 1
print(ans)
# Original solution
from collections import Counter
n,m = list(map(int,input().split()))
many  =[]
for i in range(n):
    a = list(map(int,input().split()))
    for j in range(1,a[0]+1):
        many.append(a[j])

ans = Counter(many).most_common()
count = 0
for i in range(len(ans)):
    if ans[i][1] == n:
        count += 1

print(count)

# Optimized solution
n,m = list(map(int,input().split()))
data = [0 for i in range(m)]
for i in range(n):
    k,*a = list(map(int,input().split()))
    for x in a:
        data[x-1] += 1
ans = 0
for i in range(m):
    if data[i] == n:
        ans += 1

print(ans)
# Original solution
N, M = list(map(int,input().split()))
foods = [0 for m in range(M)]
for n in range(N):
    A = list(map(int, input().split()))
    for a in A[1:]:
        foods[a-1]+=1
foods_ans = [f for f in foods if f == N]
#print(foods)
#print(foods_ans)
print((len(foods_ans)))       


# Optimized solution
N, M = list(map(int,input().split()))
foods = [0 for m in range(M)]
for n in range(N):
    A = list(map(int, input().split()))
    for a in A[1:]:
        foods[a-1]+=1
print((len([f for f in foods if f == N])))
# Original solution
N,M = list(map(int, input().split()))
KA = []
a = []
ans = 0

for i in range(N):
    KA = list(map(int, input().split()))
    A = KA[1:]
    a += A
for j in range(1,M+1):
    if a.count(j) == N:
        ans += 1
print(ans)


# Optimized solution
Ans_list = []
Ans = 0

N,M = list(map(int, input().split()))
for i in range(N):
  KA = list(map(int, input().split()))
  Mylist = KA[1:]
  for j in range(len(Mylist)):
      Ans_list.append(Mylist[j])
for i in range(M+1):
  if Ans_list.count(i)==N:
    Ans += 1
print(Ans)
# Original solution
N,M = list(map(int,input().split()))
list = [list(map(int,input().split())) for x in range(N)]

#一列目削除
for i in range(0,N):
    list[i].pop(0)

stock = []

#解答が全リストにあるか探索
for i in range(0,N):
    for j in range(0,len(list[i])):
        cnt = 0
        for k in range(0,N):
            if list[i][j] in list[k]:
                cnt += 1
        if cnt == N:
            stock.append(list[i][j])

if N == 1:
    stock = list[i]

print((len(set(stock))))

# Optimized solution
N,M = list(map(int,input().split()))

M_kinds = set(range(1,M+1))

for i in range(N):
    a,*b = list(map(int,input().split()))
    M_kinds &= set(b)

print((len(M_kinds)))
# Original solution
n, m = list(map(int, input().split()))
ref = {}

a = list(map(int, input().split()))
del a[0]
a = set(a)
ref = a

for i in range (n - 1):
    a = list(map(int, input().split()))
    del a[0]
    a = set(a)
    ref = ref & a

print((len(ref)))

# Optimized solution
n, m = list(map(int, input().split()))
cnt = [0]*m
ans = 0

for i in range(n):
    k, *a = list(map(int, input().split()))
    for j in a:
        cnt[j - 1] += 1

for i in cnt:
    if i == n:
        ans += 1

print(ans)
# Original solution
n, m = list(map(int, input().split()))
ka = [list(map(int, input().split())) for i in range(n)]
ans = [0] * m

for i in range(n):
    for j in range(1, len(ka[i])):
        for k in range(1, m+1):
            if ka[i][j] == k:
                ans[k-1] += 1
                break

print((ans.count(n)))

# Optimized solution
n, m = list(map(int, input().split()))
ka = [list(map(int, input().split())) for i in range(n)]
ans = [0] * m

for i in range(n):
    for j in ka[i][1:]:
        ans[j-1] += 1

print((ans.count(n)))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

# 大きい数字から追加していく。そのときの連結成分の大きさだけ足される。
# union findっぽく。各成分の左端を根として持つ

N = int(eval(input()))
A = [int(x) for x in input().split()]

x_to_i = {x:i for i,x in enumerate(A)}

V = set()
root = dict()
size = dict()

def find_root(x):
    y = root[x]
    if x == y:
        return y
    ry = find_root(y)
    root[x] = ry
    return ry

def merge(x,y):
    rx = find_root(x)
    ry = find_root(y)
    sx = size[rx]
    sy = size[ry]
    if sx > sy:
        rx,ry = ry,rx
        sx,sy = sy,sx
    root[rx] = ry
    size[ry] += sx

answer = 0
for x in range(N,0,-1):
    i = x_to_i[x]
    V.add(i)
    size[i] = 1
    root[i] = i
    left = 0
    right = 0
    if i-1 in V:
        left = size[find_root(i-1)]
        merge(i-1,i)
    if i+1 in V:
        right = size[find_root(i+1)]
        merge(i+1,i)
    cnt = (left+1) * (right+1)
    answer += x * cnt

print(answer)

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

"""
・大きい数から挿入していく
・連結成分の両端を管理する
"""

N,*A = list(map(int,read().split()))

A = [0] + A + [0]
ind = [0] * (N+1)
for i,x in enumerate(A):
    ind[x] = i

left = list(range(1,len(A)+1))
right = list(range(-1,len(A)-1))
answer = 0
for i in ind[:0:-1]:
    # i番目に数を挿入
    l = left[i-1]; r = right[i+1]
    left[r] = l; right[l] = r
    x = i - l + 1; y = r - i + 1
    answer += A[i] * (x * y)

print(answer)
# Original solution
import bisect
n=int(input())
a=list(map(int,input().split()))
b=sorted([(a[i],i+1) for i in range(n)])
s=[0,n+1]
ans=0
for c,j in b:
    k=bisect.bisect(s,j)
    ans+=c*(s[k]-j)*(j-s[k-1])
    s.insert(k,j)
print(ans)


# Optimized solution
n=int(input())
a=list(map(int,input().split()))
def f():
    s=[(0,-1)]
    x=[0]*(n+1)
    for i,j in enumerate(a):
        while s[-1][0]>j:s.pop()
        x[j]=i-s[-1][1]
        s.append((j,i))
    return x
l=f()
a.reverse()
r=f()
print(sum([i*l[i]*r[i] for i in a]))
# Original solution
n = int(eval(input()))
A = list(map(int, input().split()))

P = [0]*n
for i, a in enumerate(A):
    P[a-1] = i

import heapq
q = []
heapq.heapify(q)
R = [n-1]*n
for i, a in enumerate(A):
    while q and q[0]*(-1) > a:
        v = heapq.heappop(q)*(-1)
        R[v-1] = i-1
    heapq.heappush(q, a*(-1))

q = []
L = [0]*n
for j in range(n):
    i = n-1-j
    a = A[i]
    while q and q[0]*(-1) > a:
        v = heapq.heappop(q)*(-1)
        L[v-1] = i+1
    heapq.heappush(q, a*(-1))

#print(L)
#print(R)
ans = 0
for i in range(n):
    a = i+1
    l, r, p = L[i], R[i], P[i]
    ans += (p-l+1)*(r-p+1)*a
print(ans)

# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
A = [a-1 for a in A]

P = [-1]*n
for i, a in enumerate(A):
    P[a] = i

R = [n]*n
q = []
import heapq
heapq.heapify(q)
for i in range(n):
    while q:
        if -q[0] > A[i]:
            v = -heapq.heappop(q)
            R[v] = i
        else:
            break
    heapq.heappush(q, -A[i])

L = [-1]*n
q = []
heapq.heapify(q)
for i in reversed(list(range(n))):
    while q:
        if -q[0] > A[i]:
            v = -heapq.heappop(q)
            L[v] = i
        else:
            break
    heapq.heappush(q, -A[i])
#print(P)
#print(R)
#print(L)
ans = 0
for i in range(n):
    ans += (P[i]-L[i])*(R[i]-P[i])*(i+1)
print(ans)
# Original solution
#!/usr/bin/env python3
#AGC5 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
lst = [0 for i in range(n+1)]
for i in range(n):
    lst[a[i]] = i

l = [0]*n
r = [0]*n
ans = 0
for i in range(1,n+1)[::-1]:
    x = lst[i]
    if x + 1 < n and a[x+1] > i:
        R = r[x+1]
    else:
        R = x
    if x - 1 >= 0 and a[x-1] > i:
        L = l[x-1]
    else:
        L = x
    l[R] = L
    r[L] = R
    ans += (x - L + 1)*(R - x + 1)*i
print(ans)

# Optimized solution
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

"""
すべてのiに対して
「a_iが最小値となる区間はいくつあるか?」を考える.
a_iより左側でa_iより小さいもののうち最もa_iに近いものをa_l
a_iより右側でa_iより小さいもののうち最もa_iに近いものをa_rとする.
最小値がa_iとなる区間は左側がl+1,l+2,...,i
                    　右側がi,i+1,...,r-1となる区間
⇒(i-l+1)x(r-i+1)個の区間でa_iが最小値をとる
すべてのiに対してこの個数を求める.
"""

n = I()
a = LI()
lst = [0]*(n+1)

for i in range(n):
    lst[a[i]] = i

l = [0]*n #各iに対して,a_iより左側でa_iより小さいもののうち最もa_iに近いもののindex
r = [0]*n #各iに対して,a_iより右側でa_iより小さいもののうち最もa_iに近いもののindex
ans = 0

#大きい値から見ていく
for x in range(1,n+1)[::-1]:
    #調べている数 := x
    #xの位置 := i
    i = lst[x]

    #iが右端でないかつ,iの右隣が自分(x)より大きい場合
    if i+1 < n and a[i+1] > x:
        #r[i+1] := iより右側でxより小さいもののうち最もiに近いもののindex
        #  ⇒ i <= j < r[i+1] に関してa[j]はx以上である.
        R = r[i+1]
    else:
        #右隣がいないまたは右隣が自分(x)より小さい場合
        R = i
    
    #iが左端でないかつ,iの左隣が自分(x)より大きい場合
    if i-1 >= 0 and a[i-1] > x:
        #l[i-1] := iより左側でxより小さいもののうち最もiに近いもののindex
        #  ⇒ l[i-1] < j <= i に関してa[j]はx以上である.
        L = l[i-1]
    else:
        #左隣がいないまたは左隣が自分(x)より小さい場合
        L = i
    
    # L < j < Rに関してa[j]はx以上であることが分かっている.
    # ⇒Rの位置から見て左側の区間はLまで
    # ⇒Lの位置から見て右側の区間はRまで
    l[R] = L
    r[L] = R
    ans += (i-L+1)*(R-i+1)*x
print(ans)
# Original solution
import bisect
N = int(eval(input()))
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    l = 1
    for j in range(i-1,-1,-1):
        if A[i] <= A[j]:
            l += 1
        else:
            break
    r = 1
    for j in range(i+1,N):
        if A[i] <= A[j]:
            r += 1
        else:
            break
    ans += l*r*A[i]
print(ans)

# Optimized solution
N = int(eval(input()))
a = list(map(int,input().split()))
l = [i for i in range(N)]
r = [i for i in range(N)]

b = [0] * N
for i in range(N):
    b[a[i]-1] = i

ans = 0
for i in range(N-1,-1,-1):
    m = b[i]
    x,y = l[m],r[m]
    ans += (i+1)*(m-x+1)*(y-m+1)
    l[min(y+1,N-1)] = x
    r[max(x-1,0)] = y
print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):b[a[i]-1]=i
ans=0
l,r=[i for i in range(n+1)],[i for i in range(n+1)]
for i in range(n-1,-1,-1):
  ans+=(i+1)*(b[i]-l[b[i]]+1)*(r[b[i]]-b[i]+1)
  l[r[b[i]]+1],r[l[b[i]]-1]=l[b[i]],r[b[i]]
print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
l=[i-1 for i in range(n+1)]+[n]
r=[i+1 for i in range(n+1)]+[n+2]
ans=0
p=[0]*n
for i in range(n):p[a[i]-1]=i
for i in range(n-1,-1,-1):
  j=p[i]
  ans+=(i+1)*(j-l[j])*(r[j]-j)
  l[r[j]],r[l[j]]=l[j],r[j]
print(ans)
# Original solution
#木の根を求める
def find(x,par):
    if par[x] == x:
        return x
    else:
        return find(par[x],par)

#xとyの属する集合の併合
def unite(x,y,par,rank,size):
    x = find(x,par)
    y = find(y,par)
    
    if x != y:
        #xとyの属している集合が異なる時
        if rank[x] < rank[y]:
            par[x] = y
            size[y] += size[x]
        else:
            par[y] = x
            size[x] += size[y]
            if rank[x]==rank[y]:
                rank[x] += 1

#xとyが同じ集合に属するかの判定
def same(x,y,par):
    return find(x,par) == find(y,par)

########################################
n = int(eval(input()))
a = list(map(int,input().split()))

par = [0]*n #親
for i in range(n):
    par[i] = i
rank = [1]*n #深さ
size = [1]*n #size[i]:iを根とするグループのサイズ

b = [0]*(n+1) #b[i]:iがaにおいて何番目か
for i in range(n):
    b[a[i]]=i

ans = 0
for i in range(n,0,-1):
    k = b[i]
    left = 0
    right = 0
    if k-1>=0 and a[k-1]>a[k]:
        left =  size[find(k-1,par)]
        unite(k-1,k,par,rank,size)
    if k+1<=n-1 and a[k+1]>a[k]:
        right = size[find(k+1,par)]
        unite(k+1,k,par,rank,size)
    ans += (left+1)*(right+1)*a[k]

print(ans)
    

# Optimized solution
#木の根を求める
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x != y:
        #xとyの属している集合が異なる時
        if rank[x] < rank[y]:
            par[x] = y
            size[y] += size[x]
        else:
            par[y] = x
            size[x] += size[y]
            if rank[x]==rank[y]:
                rank[x] += 1

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

########################################
n = int(eval(input()))
a = list(map(int,input().split()))

par = [0]*n #親
for i in range(n):
    par[i] = i
rank = [1]*n #深さ
size = [1]*n #size[i]:iを根とするグループのサイズ

b = [0]*(n+1) #b[i]:iがaにおいて何番目か
for i in range(n):
    b[a[i]]=i

ans = 0
for i in range(n,0,-1):
    k = b[i]
    left = 0
    right = 0
    if k-1>=0 and a[k-1]>a[k]:
        left =  size[find(k-1)]
        unite(k-1,k)
    if k+1<=n-1 and a[k+1]>a[k]:
        right = size[find(k+1)]
        unite(k+1,k)
    ans += (left+1)*(right+1)*a[k]

print(ans)
# Original solution
import sys
input = sys.stdin.buffer.readline

N = int(eval(input()))
A = list(map(int, input().split()))

left = [0] * N
tmp = []
for i, a in enumerate(A):
    cnt = 1
    while tmp:
        aa, c = tmp.pop()
        if aa < a:
            tmp.append((aa, c))
            break
        else:
            cnt += c
    tmp.append((a, cnt))
    left[i] = cnt
A =A [::-1]
right = [0] * N
tmp = []
for i, a in enumerate(A):
    cnt = 1
    while tmp:
        aa, c = tmp.pop()
        if aa < a:
            tmp.append((aa, c))
            break
        else:
            cnt += c
    tmp.append((a, cnt))
    right[i] = cnt
right = right[::-1]

A = A[::-1]
answer = 0
for i in range(N):
    answer += left[i] * right[i] * A[i]
    
print(answer)

# Optimized solution
import sys
input = sys.stdin.buffer.readline

N = int(eval(input()))
A = list(map(int, input().split()))

def while_pop(arr):
    res = [0] * N
    tmp = []
    for i, a in enumerate(arr):
        cnt = 1
        while tmp:
            aa, c = tmp.pop()
            if aa < a:
                tmp.append((aa, c))
                break
            else:
                cnt += c
        tmp.append((a, cnt))
        res[i] = cnt
    return res

left = while_pop(A)
right = while_pop(A[::-1])[::-1]

answer = 0
for i in range(N):
    answer += left[i] * right[i] * A[i]
    
print(answer)