Why must only mod at the end???
If I comment out those two ans %= mod in the dfs, there will be a long case that cannot pass, why???\n\n\nclass Solution {\n    private long mod = (long) (1e9 +
If I comment out those two `ans %= mod` in the dfs, there will be a long case that cannot pass, why???\n\n```\nclass Solution {\n    private long mod = (long) (1e9 + 7);\n    private Map<Integer, Integer> map1 = new HashMap<>(), map2 = new HashMap<>();\n    public int maxSum(int[] nums1, int[] nums2) {\n        int m = nums1.length, n = nums2.length;\n        for (int i = 0; i < m; i++) {\n            map1.put(nums1[i], i);\n        }\n        \n        for (int i = 0; i < n; i++) {\n            map2.put(nums2[i], i);\n        }\n\n        int len = Math.max(m, n);\n        long[][] dp = new long[len][2];\n        \n        return (int) Math.max(dfs(nums1, nums2, 0, 0, dp) % mod, dfs(nums1, nums2, 0, 1, dp) % mod);\n    }\n    \n    private long dfs(int[] a, int[] b, int p, int flag, long[][] dp) {\n        if ((flag == 0 && p == a.length) || (flag == 1 && p == b.length)) return 0;\n        \n        if (dp[p][flag] != 0) return dp[p][flag];\n        \n        long ans = 0;\n        \n        if (flag == 0) {\n            if (map2.containsKey(a[p])) {\n                int index = map2.get(a[p]);\n                ans = (a[p] + dfs(a, b, index + 1, 1, dp));\n            } \n            \n            ans = Math.max(ans, a[p] + dfs(a, b, p + 1, flag, dp));\n            // ans %= mod;\n        } else {\n            if (map1.containsKey(b[p])) {\n                int index = map1.get(b[p]);\n                ans = b[p] + dfs(a, b, index + 1, 0, dp);\n            } \n            \n            ans = Math.max(ans, b[p] + dfs(a, b, p + 1, flag, dp));\n            // ans %= mod;\n        }\n\t\t\n        return dp[p][flag] = ans;\n    }\n}\n```
[]