Editorial Solution /  Typescript / Pascal Triangle
\n\nfunction numOfWays(nums: number[]): number {\n  const mod = BigInt(10 ** 9 + 7);\n  const table: number[][] = [];\n\n  // Fill Pascal Table\n  for (let i = 
# \n```\nfunction numOfWays(nums: number[]): number {\n  const mod = BigInt(10 ** 9 + 7);\n  const table: number[][] = [];\n\n  // Fill Pascal Table\n  for (let i = 0; i < nums.length; i++) {\n    table[i] = new Array(i + 1).fill(1);\n    for (let j = 1; j < nums.length; j++) {\n      if (j > i) continue;\n      const a = table[i - 1][j - 1] ?? 0;\n      const b = table[i - 1][j] ?? 0;\n      table[i][j] = (a + b) % Number(mod);\n    }\n  }\n\n  function getWaysHelper(nums: number[]): bigint {\n    const m = nums.length;\n    if (m < 3) return 1n;\n\n    const leftNodes: number[] = [];\n    const rightNodes: number[] = [];\n\n    for (let i = 1; i < m; i++) {\n      if (nums[i] < nums[0]) leftNodes.push(nums[i]);\n      else rightNodes.push(nums[i]);\n    }\n\n    const leftWays = getWaysHelper(leftNodes) % mod;\n    const rightWays = getWaysHelper(rightNodes) % mod;\n\n    return (((leftWays * rightWays) % mod) * BigInt(table[m - 1][leftNodes.length])) % mod;\n  }\n\n  const ways = getWaysHelper(nums) - 1n; // -1 original order\n\n  return Number(ways % mod);\n}\n```
['TypeScript']