#!python3 from math import factorial ways = {} m = [] mn = 0 modval = 10**9 + 7 def split_seq_at(ix_l): global ways,m,mn ways_acc = 0 last = 0 for w in range(1,mn+1): if ix_l+w > mn: ways[ (ix_l,w) ] = ways_acc continue last = ix_l+w ns = m[ix_l:ix_l+w] ns2 = sorted(ns) if ns != ns2: ways[ (ix_l,w) ] = ways_acc continue ways_this = factorial(len(ns2)) prev_v = ns2[0] c = 1 for v in ns2[1:]: if v != prev_v: c = 1 prev_v = v else: c += 1 ways_this //= c k = (ix_l+w,w) #print(ways,(ix_l,w),k,ways_acc,ways_this) if k in ways: v = ways_this * ways[k] v %= modval ways_acc += v else: ways_acc += 1 ways_acc %= modval ways[ (ix_l,w) ] = ways_acc #for w in range(last,mn+1): ways[ (ix_l,w) ] = ways_acc return ways_acc def sama(ns): global ways,m,mn ways = {} m = ns mn = len(m) for ix_r in range(mn-1,-1,-1): v = split_seq_at(ix_r) return v def run(): _ = int(input()) ns = list(map(int,input().split())) print(sama(ns)) run()