#!/bin/python3 import sys n = int(input().strip()) m = list(map(int, input().strip().split(' '))) MOD = int(1e9 + 7) count = 1 pre = m[0] arrs = [] for x in m[1:]: if x < pre: arrs.append(count) count = 1 else: count += 1 arrs.append(count) maxlen = max(arrs) f = {} per = {} def initper(n): count = 1 for x in range(0, n): count = (count * (n - x)) % MOD; per[(n, x + 1)] = count for x in range(1, maxlen + 1): initper(x) def getf(n, up, down): try: return f[(n, up, down)] except: pass if up < down or n < down: return 0 if n == down: f[(n, up, down)] = per[(up, down)] return f[(n, up, down)] if n < 2 * down: return 0 ret = 0 for x in range(down, min(up, n - down) + 1): ret = (ret + getf(n - down, up, x) * per[(x, down)] ) % MOD; f[(n, up, down)] = ret return ret def dp(n, i): if i >= len(arrs): return 1 ret = 0 for x in range(1, min(n, arrs[i] // 2) + 1): ret = (ret + getf(arrs[i], n, x) * dp(x, i + 1)) % MOD; if arrs[i] <= n: ret = (ret + per[(n, arrs[i])] * dp(arrs[i], i + 1)) % MOD; return ret firste = arrs[0] ans = dp(arrs[0], 1) for x in range(1, arrs[0]): arrs[0] = firste - x #print(x, dp(x, 0)) ans = (ans + dp(x, 0)) % MOD #print('f 2 1 1 :', f[(2,1,1)] ) #print('f 1 1 1 :', f[(1,1,1)] ) print(ans) # your code goes here