on the fly
Instead of counting and then computing factorials and their product, just compute the product directly while counting.\n\n    public int numPrimeArrangements(in
Instead of counting and then computing factorials and their product, just compute the product directly while counting.\n```\n    public int numPrimeArrangements(int n) {\n        int[] non = new int[n+1];\n        int[] cnt = {0, 1};\n        long res = 1;\n        for (int i=2; i<=n; i++) {\n            if (non[i] == 0)\n                for (int m=i*i; m<=n; m+=i)\n                    non[m] = 1;\n            res = res * ++cnt[non[i]] % 1_000_000_007;\n        }\n        return (int) res;\n    }\n```
[]