using System; using System.Linq; using System.Collections.Generic; class Solution { static long mod = 1000000007; static long[] fac = new long[1201]; static long[,] binom = new long[1201,1201]; public static void Main() { fac [0] = 1; binom [0, 0] = 1; for (int i = 1; i < fac.Length; i++) { fac [i] = fac [i - 1] * i; fac [i] %= mod; binom [i, 0] = 1; for (int j = 1; j <= i; j++) { binom [i, j] = (binom [i - 1, j] + binom [i - 1, j - 1]) % mod; } } Console.ReadLine (); int[] nums = Console.ReadLine ().Split (' ').Select (int.Parse).ToArray (); Console.WriteLine (Solve (nums, 0, nums.Length)); } static long[,] cache = new long[1201, 1201]; static long Solve(int[] nums, int index, int maxLen) { if (nums.Length == index) return 1; if (cache [index, maxLen] > 0) return cache [index, maxLen]; long result = 0; for (int part = 1; part <= maxLen && index + part <= nums.Length; part++) { if (part > 1 && nums [index + part - 1] < nums [index + part - 2]) break; //no sorted subsection long tmp = Solve (nums, index + part, part); if (index > 0) { tmp *= fac [part]; tmp %= mod; tmp *= binom [maxLen, part]; } result += tmp; result %= mod; } cache [index, maxLen] = result % mod; return cache [index, maxLen]; } }