This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of m and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one.

There are n+1 distinct colours in the universe, numbered 0 through n. There is a strip of paper m centimetres long initially painted with colour 0. 

Alice took a brush and painted the strip using the following process. For each i from 1 to n, in this order, she picks two integers 0 ≤ a_i < b_i ≤ m, such that the segment [a_i, b_i] is currently painted with a single colour, and repaints it with colour i. 

Alice chose the segments in such a way that each centimetre is now painted in some colour other than 0. Formally, the segment [i-1, i] is painted with colour c_i (c_i ≠ 0). Every colour other than 0 is visible on the strip.

Count the number of different pairs of sequences \\{a_i\}_{i=1}^n, \\{b_i\}_{i=1}^n that result in this configuration. 

Since this number may be large, output it modulo 998244353.

Input

The first line contains a two integers n, m (1 ≤ n ≤ 500, n ≤ m ≤ 10^6) — the number of colours excluding the colour 0 and the length of the paper, respectively.

The second line contains m space separated integers c_1, c_2, …, c_m (1 ≤ c_i ≤ n) — the colour visible on the segment [i-1, i] after the process ends. It is guaranteed that for all j between 1 and n there is an index k such that c_k = j.

Output

Output a single integer — the number of ways Alice can perform the painting, modulo 998244353.

Examples

Input


3 3
1 2 3


Output


5


Input


2 3
1 2 1


Output


1


Input


2 3
2 1 2


Output


0


Input


7 7
4 5 1 6 2 3 7


Output


165


Input


8 17
1 3 2 2 7 8 2 5 5 4 4 4 1 1 6 1 1


Output


20

Note

In the first example, there are 5 ways, all depicted in the figure below. Here, 0 is white, 1 is red, 2 is green and 3 is blue.

<image>

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour 2.

<image>

In the second example, Alice must first paint segment 0 3 with colour 1 and then segment 1 2 with colour 2. 
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.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author ilyakor
 */
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);
        TaskF1 solver = new TaskF1();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF1 {
        static final int mod = 998244353;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] a = new int[m];
            final int[] L = new int[n + 1];
            Arrays.fill(L, 1000 * 1000 * 1000);
            int[] R = new int[n + 1];
            Arrays.fill(R, -1000 * 1000 * 1000);
            for (int i = 0; i < m; ++i) {
                a[i] = in.nextInt();
                if (L[a[i]] > i) L[a[i]] = i;
                if (R[a[i]] < i) R[a[i]] = i;
            }
            RMQSparseTable rmq = new RMQSparseTable(a);
            for (int i = 1; i <= n; ++i) {
                if (R[i] < 0) {
                    out.printLine(0);
                    return;
                }
                int j = rmq.minPos(L[i], R[i]);
                if (a[j] < i) {
                    out.printLine(0);
                    return;
                }
            }

            int[] parent = new int[n + 1];
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j < i; ++j)
                    if (L[j] <= L[i] && R[i] <= R[j])
                        parent[i] = j;
            ArrayList<Integer>[] kids = new ArrayList[n];
            for (int i = 0; i < n; ++i)
                kids[i] = new ArrayList<>();
            for (int i = 1; i <= n; ++i)
                kids[parent[i]].add(i);
            long res = 1;
            for (int i = 0; i < n; ++i) {
                if (kids[i].isEmpty()) continue;
                Collections.sort(kids[i], new Comparator<Integer>() {

                    public int compare(Integer x, Integer y) {
                        return Integer.compare(L[x], L[y]);
                    }
                });
                ArrayList<Integer> buf = new ArrayList<>();
                for (int x : kids[i]) {
                    if (buf.isEmpty() || R[buf.get(buf.size() - 1)] + 1 == L[x]) {
                        buf.add(x);
                    } else {
                        res *= calc(buf);
                        res %= mod;
                        buf.clear();
                        buf.add(x);
                    }
                }
                if (!buf.isEmpty()) {
                    res *= calc(buf);
                    res %= mod;
                }
            }
            out.printLine(res);
        }

        private long calc(ArrayList<Integer> _a) {
            int n = _a.size();
            int[] a = new int[n];
            for (int i = 0; i < n; ++i)
                a[i] = _a.get(i);
            int[][] d = new int[n + 1][n + 1];
            for (int i = 0; i < n; ++i) {
                d[i + 1][i] = 1;
            }
            for (int len = 1; len <= n; ++len)
                for (int i = 0; i + len <= n; ++i) {
                    int j = i + len - 1;
                    if (len <= 1) {
                        d[i][j] = 1;
                        continue;
                    }
                    int pos = i;
                    for (int k = i; k <= j; ++k)
                        if (a[k] < a[pos])
                            pos = k;
                    long cl = 0;
                    for (int l = i; l <= pos; ++l) {
                        long val = l == i ? 1 : d[i][l - 1];
                        val *= l == pos ? 1 : d[l][pos - 1];
                        cl += val;
                        cl %= mod;
                    }
                    long cr = 0;
                    for (int r = pos; r <= j; ++r) {
                        long val = r == j ? 1 : d[r + 1][j];
                        val *= r == pos ? 1 : d[pos + 1][r];
                        cr += val;
                        cr %= mod;
                    }
                    d[i][j] = (int) ((cl * cr) % mod);
                }
            return d[0][n - 1];
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buffer = new byte[10000];
        private int cur;
        private int count;

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

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

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

        public int readSkipSpace() {
            int c;
            do {
                c = read();
            } while (isSpace(c));
            return c;
        }

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

    }

    static class RMQSparseTable {
        int[] logTable;
        int[][] rmq;
        int[] a;

        public RMQSparseTable(int[] a) {
            this.a = a;
            int n = a.length;

            logTable = new int[n + 1];
            for (int i = 2; i <= n; i++)
                logTable[i] = logTable[i >> 1] + 1;

            rmq = new int[logTable[n] + 1][n];

            for (int i = 0; i < n; ++i)
                rmq[0][i] = i;

            for (int k = 1; (1 << k) < n; ++k) {
                for (int i = 0; i + (1 << k) <= n; i++) {
                    int x = rmq[k - 1][i];
                    int y = rmq[k - 1][i + (1 << k - 1)];
                    rmq[k][i] = a[x] <= a[y] ? x : y;
                }
            }
        }

        public int minPos(int i, int j) {
            int k = logTable[j - i];
            int x = rmq[k][i];
            int y = rmq[k][j - (1 << k) + 1];
            return a[x] <= a[y] ? x : y;
        }

    }

    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 print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

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

    }
}

