Problem: This is the hard version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved.

A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it.

Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j.

Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions:

  * p is lexicographically smaller than q. 
  * the number of inversions in p is greater than the number of inversions in q. 



Print the number of such pairs modulo mod. Note that mod may not be a prime.

Input

The only line contains two integers n and mod (1≤ n≤ 500, 1≤ mod≤ 10^9).

Output

Print one integer, which is the answer modulo mod.

Example

Input


4 403458273


Output


17

Note

The following are all valid pairs (p,q) when n=4.

  * p=[1,3,4,2], q=[2,1,3,4], 
  * p=[1,4,2,3], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,4,3], 
  * p=[1,4,3,2], q=[2,3,1,4], 
  * p=[1,4,3,2], q=[3,1,2,4], 
  * p=[2,3,4,1], q=[3,1,2,4], 
  * p=[2,4,1,3], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,4,2], 
  * p=[2,4,3,1], q=[3,2,1,4], 
  * p=[2,4,3,1], q=[4,1,2,3], 
  * p=[3,2,4,1], q=[4,1,2,3], 
  * p=[3,4,1,2], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,3,2], 
  * p=[3,4,2,1], q=[4,2,1,3]. 

Solution (in JAVA):

Solution:
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

public class P1542E2 {

    private static long app(final int n, final long M) {
        final long[] C = new long[n+1];
        C[n] = 1;
        for (int k = n-1; k >= 0; k--) {
            C[k] = C[k+1] * (k+1) % M;
            if (C[k] == 0) {
                break;
            }
        }

        long s = 0;
        long[] K = {1, 1};
        int l = 2;
        for (int i = 4; i < n+1; i++) {
            final int w = i-1;
            final int l_ = l + w - 1;
            final long[] K_ = new long[l_];
            K_[0] = K[0];
            final int mlw = Math.min(l, w);
            for (int j = 1; j < mlw; j++) {
                K_[j] = (K_[j-1] + K[j]) % M;
            }
            for (int j = mlw; j < l; j++) {
                K_[j] = (K_[j-1] + K[j] - K[j-w]) % M;
            }
            for (int j = l; j < l_; j++) {
                K_[j] = K_[l_ - j - 1];
            }
            K = K_;
            l = l_;

            if (C[i] == 0) {
                continue;
            }

            final long[] S1 = new long[l];
            S1[0] = K[0];
            for (int j = 1; j < l; j++) {
                S1[j] = (S1[j-1] + K[j]) % M;
            }

            final long[] S2 = new long[l];
            S2[0] = S1[0];
            for (int j = 1; j < l; j++) {
                S2[j] = (S2[j-1] + S1[j]) % M;
            }

            final long[] S3 = new long[l];
            S3[0] = S2[0];
            for (int j = 1; j < l; j++) {
                S3[j] = (S3[j-1] + S2[j]) % M;
            }

            long si = K[2] * S2[0] % M * (i-1) % M;
            for (int j = 3; j < l; j++) {
                final int j_ = j - 2;
                long sij = S2[j_] * (i-1) % M;
                sij -= S3[j_ - 1];
                if (j_ >= i) {
                    sij += S3[j_ - i];
                }
                sij %= M;
                if (sij < 0) {
                    sij += M;
                }
                sij *= K[j];
                sij %= M;
                si += sij;
                si %= M;
            }
            si *= C[i];
            si %= M;
            s += si;
            s %= M;
        }
        return s;
    }

    public static void main(String[] args) throws IOException {
        try {
            final FastReader fr = new FastReader(System.in);
            final int n = fr.readInt();
            final long M = fr.readLong();
            System.out.println(app(n, M));
        } catch (Throwable t) {
            t.printStackTrace(System.out);
            System.exit(1);
        }
    }

    private static final void log(Object o) {
        System.err.println(o);
    }

    private static class FastReader implements Closeable {
        private static final byte B_SP = ' ';
        private static final byte B_CR = '\r';
        private static final byte B_LF = '\n';
        private static final byte B_0 = '0';
        private static final byte B_9 = '9';
        private static final byte B_DASH = '-';

        private final InputStream in;
        private byte prev;

        public FastReader(InputStream in) {
            this.in = in;
        }

        public byte read() throws IOException {
            return (prev = (byte) in.read());
        }

        public byte readNonWs() throws IOException {
            byte b;
            do {
                b = read();
            } while (b <= B_SP);
            return b;
        }

        public int readInt() throws IOException {
            byte b = readNonWs();
            final boolean neg = (b == B_DASH);
            if (neg) {
                b = read();
            }
            int i = (b - B_0);
            b = read();
            while (b >= B_0 && b <= B_9) {
                i = i * 10 + (b - B_0);
                b = read();
            }
            return neg ? -i : i;
        }

        public long readLong() throws IOException {
            byte b = readNonWs();
            final boolean neg = (b == B_DASH);
            if (neg) {
                b = read();
            }
            long i = (b - B_0);
            b = read();
            while (b >= B_0 && b <= B_9) {
                i = i * 10 + (b - B_0);
                b = read();
            }
            return neg ? -i : i;
        }

        public int readToken(byte[] tbuf) throws IOException {
            byte b = readNonWs();
            int i = 0;
            do {
                tbuf[i++] = b;
                b = read();
            } while (b >= B_SP);
            return i;
        }

        public int readLine(byte[] lbuf) throws IOException {
            byte b;
            if (prev == B_CR) {
                b = read();
                if (b == B_LF) {
                    b = read();
                }
            } else {
                b = read();
            }
            int i = 0;
            while (b >= B_SP) {
                lbuf[i++] = b;
                b = read();
            }
            return i;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    }
}