Java Recursion with Memoization (3-dimensional DP)
\nclass Solution {\n    final int MOD = 1_000_000_007;\n    long res = 0;\n    \n    public int dieSimulator(int n, int[] rollMax) {\n        int max = 0;\n    
```\nclass Solution {\n    final int MOD = 1_000_000_007;\n    long res = 0;\n    \n    public int dieSimulator(int n, int[] rollMax) {\n        int max = 0;\n        for (int m: rollMax) {\n            max = Math.max(max, m);\n        }\n        \n        Long dp[][][] = new Long[n + 1][max + 1][max + 1];   // dp[i][j][k] means using the i-th dice with roll max j \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t // and currently there are k same continuous dice, \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t // how many sequences can form in total\n        for (int i = 0; i < 6; i++) {\n            res += dfs(i, 1, 1, rollMax, n, dp) % MOD;  // get the sum of using each of [0:5] as the first dice\n            res %= MOD;\n        }\n\t\t\n        return (int) res;\n    }\n    \n    private long dfs(int curr, int dice, int count, int[] rollMax, int n, Long dp[][][]) {  // curr is the current dice number from [0:5],\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// dice is the i-th dice using in this step,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// count is the contiuous count of this dice\n        if (dice == n) {\n            return 1;\n        }\n        if (dp[dice][rollMax[curr]][count] != null) {  // the total number of sequences formed by th i-th dice with the same continuous count \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t// and roll max has already been calculated, regardless of which dice number it is\n            return dp[dice][rollMax[curr]][count];\n        }\n        \n        long ans = 0;\n        for (int i = 0; i < 6; i++) {\n            if (i == curr) {\n                if (count >= rollMax[curr]) continue;\n                    \n                ans += (dfs(i, dice + 1, count + 1, rollMax, n, dp)) % MOD;\n            } else {\n                ans += (dfs(i, dice + 1, 1, rollMax, n, dp)) % MOD;\n            }\n            ans %= MOD;\n        }\n\t\t\n        dp[dice][rollMax[curr]][count] = ans;\n        return ans;\n    }\n}\n```
[]