JAVA | 100 % TIME AND SPACE | USING SIEVE |
\'\'\'\nclass Solution {\n    \n    static final int mod=1000000007;\n    \n    public int numPrimeArrangements(int n) {\n        \n        boolean p[]=new bool
\'\'\'\nclass Solution {\n    \n    static final int mod=1000000007;\n    \n    public int numPrimeArrangements(int n) {\n        \n        boolean p[]=new boolean[n+1];\n        \n    \n        \n        isprime(p,n+1);\n        \n        int x=2;\n        int count=0;\n        \n        while(x<=n)\n        {\n            if(p[x])\n            {count++;\n            \n            }\n            x++;\n        }\n    \n      return (int)((fact(count)*fact(n-count))%mod);\n        \n   \n    }\n    \n    \n    public long fact(int num){\n        \n      long res=1;\n        \n        for(int i=num;i>=1;i--)\n            res=(res*i)%mod;\n            \n        return res;\n    }\n    \n    \n    public void isprime(boolean p[],int num){\n        \n        Arrays.fill(p,true);\n      \n        for(int i=2;i*i<num;i++)\n        {\n            if(p[i])\n            {\n                for(int j=i*i;j<num;j+=i)\n                    p[j]=false;\n            }\n\n        }        \n    }\n}\n\'\'\'
[]