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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        E2AbnormalPermutationPairsHardVersion solver = new E2AbnormalPermutationPairsHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E2AbnormalPermutationPairsHardVersion {
        ModInt mod;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            mod = new ModInt(m);
            int[] factorial = new int[n + 1];
            factorial[0] = n;
            for (int i = 1; i < n; i++) factorial[i] = mod.multiply(factorial[i - 1], n - i);
            int[] dp = new int[n * (n - 1) + 10 * n];
            int middle = (n * (n - 1)) / 2 + 5 * n;
            int[] temp = dp.clone();
            int[] temp2 = dp.clone();
            dp[middle] = 1;
            int answer = 0;
            for (int i = n - 1; i >= 0; i--) {
                int c = n - 1 - i;
                temp = dp.clone();
                int limit = (c * (c - 1)) / 2;
                int count = 0;

                temp[middle + limit + 1] = temp2[middle + limit + 1] = 0;
                for (int j = middle + limit; j >= middle - limit; j--) {
                    temp[j] = mod.add(temp[j + 1], dp[j]);
                    temp2[j] = mod.add(temp2[j + 1], mod.multiply(dp[j], j));
                }
                for (int j = middle - limit - 1; j >= 0; j--) {
                    temp[j] = temp[middle - limit];
                    temp2[j] = temp2[middle - limit];
                }

                for (int cur = -c; cur < 0; cur++) {
                    int pairs = c + cur + 1;
                    count = mod.add(count, mod.multiply(temp[middle - cur + 1], pairs));
                }
                Arrays.fill(dp, 0);
                answer = mod.add(answer, mod.multiply(count, i == 0 ? 1 : factorial[i - 1]));
                int limit2 = (c * (c + 1)) / 2;
                for (int j = middle - limit2; j <= middle; j++) {
                    dp[j] = mod.multiply(c + 1, mod.subtract(temp[j - c], temp[j + c + 1]));

                    int sum_1 = mod.subtract(temp[j - c], temp[j + 1]);
                    int sum_2 = mod.subtract(temp2[j - c], temp2[j + 1]);
                    dp[j] = mod.subtract(dp[j], mod.subtract(mod.multiply(j, sum_1), sum_2));
                    sum_1 = mod.subtract(temp[j + 1], temp[j + c + 1]);
                    sum_2 = mod.subtract(temp2[j + 1], temp2[j + c + 1]);
                    dp[j] = mod.subtract(dp[j], mod.subtract(sum_2, mod.multiply(j, sum_1)));
                }
                for (int j = 1; j <= limit2; j++) dp[middle + j] = dp[middle - j];
            }
            out.println(answer);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.print('\n');
        }

        public void close() {
            writer.close();
        }

    }

    static class ModInt {
        int modulo;

        public ModInt(int m) {
            modulo = m;
        }

        public int add(int x, int y) {
            x += y;
            if (x >= modulo) x -= modulo;
            return x;
        }

        public int subtract(int x, int y) {
            x -= y;
            if (x < 0) x += modulo;
            return x;
        }

        public int multiply(int x, int y) {
            return (int) ((x * 1L * y) % modulo);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}