description: You are given an array of n positive integers a_1, a_2, …, a_n. Your task is to calculate the number of arrays of n positive integers b_1, b_2, …, b_n such that: 
  
    * 1 ≤ b_i ≤ a_i for every i (1 ≤ i ≤ n), and 
    * b_i ≠ b_{i+1} for every i (1 ≤ i ≤ n - 1). 
  
  
  
  The number of such arrays can be very large, so print it modulo 998 244 353.
  
  Input
  
  The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array a.
  
  The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).
  
  Output
  
  Print the answer modulo 998 244 353 in a single line.
  
  Examples
  
  Input
  
  
  3
  2 2 2
  
  
  Output
  
  
  2
  
  Input
  
  
  2
  2 3
  
  
  Output
  
  
  4
  
  Input
  
  
  3
  1 1 1
  
  
  Output
  
  
  0
  
  Note
  
  In the first test case possible arrays are [1, 2, 1] and [2, 1, 2].
  
  In the second test case possible arrays are [1, 2], [1, 3], [2, 1] and [2, 3].
solution: import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStreamWriter;
  import java.math.BigInteger;
  
  import java.math.BigDecimal;
  import java.math.MathContext;
  import java.math.RoundingMode;
  
  
  import java.util.ArrayDeque;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Locale;
  import java.util.Map.Entry;
  import java.util.PriorityQueue;
  import java.util.Random;
  import java.util.Scanner;
  import java.util.TreeSet;
  
  
  
  public final class CF_759_D2_F2{
  
  	static boolean verb=true;
  	static void log(Object X){if (verb) System.err.println(X);}
  	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
  	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
  	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
  	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}
  
  	static void logWln(Object X){if (verb) System.err.print(X);}
  	static void info(Object o){	System.out.println(o);}
  	static void output(Object o){outputWln(""+o+"\n");	}
  	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}
  
  
  	// Global vars
  	static BufferedWriter out;
  	static InputReader reader;
  
  	static long powerMod(long b,long e,long m){
  		long x=1;
  		while (e>0) {
  			if (e%2==1)
  				x=(b*x)%m;
  			b=(b*b)%m;
  			e=e/2;
  		}
  		return x; 
  	}
  
  
  
  	static void test() {
  		log("testing");
  		Random r=new Random();
  		int NTESTS=10000;
  		int NMAX=100;
  		//int MMAX=10000;
  		int VMAX=100;
  
  		for( int t=0;t<NTESTS;t++) {
  			int n=4;
  			int[] a=new int[n];
  			for (int i=0;i<n;i++)
  				a[i]=r.nextInt(VMAX)+1;
  			long x=solveSubtil(a);
  			long y=solveBourrin4(a[0],a[1],a[2],a[3]);
  			if (x!=y) {
  				log("Error");
  				log(n);
  				log(a);
  				log(x);
  				log(y);
  				return;
  			}
  		}
  		log("done");
  	}
  
  
  
  
  
  	static int sign(int x) {
  		if (x>0)
  			return 1;
  		if (x==0)
  			return 0;
  		return -1;
  	}
  
  
  
  	static class Segment implements Comparable<Segment>{
  		int a;
  		int b;
  
  		public int compareTo(Segment X) {
  			if (a!=X.a)
  				return a-X.a;
  			if (b!=X.b)
  				return b-X.b;
  			return 0;
  		}
  
  		public Segment(int a, int b) {
  			this.a = a;
  			this.b = b;
  		}
  
  
  
  	}
  
  
  
  	// REVERSE
  	static class Composite implements Comparable<Composite>{
  		int x;
  		int t;
  
  		public int compareTo(Composite X) {
  			if (x!=X.x)
  				return -(x-X.x);
  			return t-X.t;
  		}
  
  		public Composite(int x, int t) {
  			this.x = x;
  			this.t = t;
  		}
  
  
  	}
  	static class BIT {
  
  		int[] tree;
  		int N;
  
  		BIT(int N){
  			tree=new int[N+1];
  			this.N=N;
  		}
  
  		void add(int idx,int val){
  			idx++;
  			while (idx<=N){
  				tree[idx]+=val;
  				idx+=idx & (-idx);
  			}
  		}
  		int read(int idx){
  			idx++;
  			int sum=0;
  			while (idx>0){
  				sum+=tree[idx];
  				idx-=idx & (-idx);
  			}
  			return sum;
  		}
  
  	}
  
  
  
  	// reverse segment --> list of segments 
  
  	static int mod=998244353;
  
  
  	// segment, list of hight and density
  
  	// case where x is smaller
  	static ArrayList<Integer> mergeSmaller(ArrayList<Integer> list,int x){
  		//log("merge smaller");
  		//log(list);
  		//log(x);
  		int prev=0;
  		int L=list.size();
  		int i=0;
  		ArrayList<Integer> nxt=new ArrayList<Integer>();
  
  		long total=0;
  		for (int e=0;e<L;e+=2) {
  			long h=list.get(e);
  			if (e>0)
  				h-=list.get(e-2);
  			h%=mod;
  			long dens=list.get(e+1)%mod;
  			long v=h*dens;
  			v%=mod;
  			total+=v;
  			total%=mod;
  		}
  
  		while (i<L && list.get(i)<=x) {
  			int y=list.get(i);
  			nxt.add(y);
  			long density=list.get(i+1)%mod;
  			// remove one point
  			long ptot=total-density;
  			if (ptot<0)
  				ptot+=mod;
  			nxt.add((int)ptot);
  			i+=2;
  			prev=y;
  		}
  		if (i<L) {
  			// in case we need to split just for x
  			if (prev<x) {
  				// split in to parts
  				int y=x;
  				nxt.add(y);
  				long density=list.get(i+1)%mod;
  				// remove one point
  				long ptot=total-density;
  				if (ptot<0)
  					ptot+=mod;
  				nxt.add((int)ptot);
  
  			}
  		}
  
  		return nxt;
  	}
  
  
  	// case where x is higher
  	static ArrayList<Integer> mergeHigher(ArrayList<Integer> list,int x){
  
  		int prev=0;
  		int L=list.size();
  		int i=0;
  		ArrayList<Integer> nxt=new ArrayList<Integer>();
  
  		long total=0;
  		for (int e=0;e<L;e+=2) {
  			long h=list.get(e);
  			if (e>0)
  				h-=list.get(e-2);
  			h%=mod;
  			long dens=list.get(e+1)%mod;
  			long v=h*dens;
  			v%=mod;
  			total+=v;
  			total%=mod;
  		}
  
  
  		while (i<L) {
  			int y=list.get(i);
  			nxt.add(y);
  			long density=list.get(i+1)%mod;
  			// remove one point
  			long ptot=total-density;
  			if (ptot<0)
  				ptot+=mod;
  			nxt.add((int)ptot);
  			i+=2;
  			prev=y;
  		}
  		// now add the remaining
  		if (x>prev) {
  			nxt.add(x);
  			nxt.add((int)total);
  		}
  
  		return nxt;
  	}
  
  	// case where x is larger
  
  
  	static long solveSubtil(int[] a) {
  		int n=a.length;
  		ArrayList<Integer> lst=new ArrayList<Integer>();
  		lst.add(a[0]);
  		lst.add(1);
  		for (int i=1;i<n;i++) {
  			if (a[i]<=a[i-1]) {
  				lst=mergeSmaller(lst,a[i]);
  			} else 
  				lst=mergeHigher(lst,a[i]);
  		}
  		//log(lst);
  		int L=lst.size();
  		long total=0;
  		for (int e=0;e<L;e+=2) {
  			long h=lst.get(e);
  			if (e>0)
  				h-=lst.get(e-2);
  			long dens=lst.get(e+1);
  			long v=h*dens;
  			v%=mod;
  			total+=v;
  			total%=mod;
  		}
  		return total;
  	}
  
  	static long solveBourrin3(int a,int b,int c) {
  		long ans=0;
  		for (int e=1;e<=a;e++)
  			for (int f=1;f<=b;f++)
  				for (int g=1;g<=c;g++)
  					if (e!=f && f!=g)
  						ans++;
  		return ans;
  	}
  	
  	static long solveBourrin4(int a,int b,int c,int d) {
  		long ans=0;
  		for (int e=1;e<=a;e++)
  			for (int f=1;f<=b;f++)
  				for (int g=1;g<=c;g++)
  					for (int h=1;h<=d;h++)
  					if (e!=f && f!=g && g!=h) {
  						ans++;
  						ans%=mod;
  					}
  		return ans;
  	}
  
  	static void process() throws Exception {
  
  
  
  		out = new BufferedWriter(new OutputStreamWriter(System.out));
  		reader=new InputReader(System.in);
  		Locale.setDefault(Locale.US);
  		//Scanner sc=new Scanner(System.in);
  		//sc.useLocale(Locale.US);
  
  
  		//test();
  
  		int n=reader.readInt();
  		int[] a=new int[n];
  		for (int i=0;i<n;i++)
  			a[i]=reader.readInt();
  
  		ArrayList<Integer> lst=new ArrayList<Integer>();
  		lst.add(a[0]);
  		lst.add(1);
  		for (int i=1;i<n;i++) {
  			if (a[i]<=a[i-1]) {
  				lst=mergeSmaller(lst,a[i]);
  			} else 
  				lst=mergeHigher(lst,a[i]);
  		}
  		//log(lst);
  		int L=lst.size();
  		long total=0;
  		for (int e=0;e<L;e+=2) {
  			long h=lst.get(e);
  			if (e>0)
  				h-=lst.get(e-2);
  			long dens=lst.get(e+1);
  			long v=h*dens;
  			v%=mod;
  			total+=v;
  			total%=mod;
  		}
  		output(total);
  
  		try {
  			out.close();
  		}
  		catch (Exception EX){}
  
  
  	}
  
  
  	public static void main(String[] args) throws Exception {
  		process();
  
  	}
  
  	static final class InputReader {
  		private final InputStream stream;
  		private final byte[] buf = new byte[1024];
  		private int curChar;
  		private int numChars;
  
  		public InputReader(InputStream stream) {
  			this.stream = stream;
  		}
  
  		private int read() throws IOException {
  			if (curChar >= numChars) {
  				curChar = 0;
  				numChars = stream.read(buf);
  				if (numChars <= 0) {
  					return -1;
  				}
  			}
  			return buf[curChar++];
  		}
  
  
  		public final String readString() throws IOException {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res=new StringBuilder();
  			do {
  				res.append((char)c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		public final int readInt() throws IOException {
  			int c = read();
  			boolean neg=false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d=(char)c;
  			//log("d:"+d);
  			if (d=='-') {
  				neg=true;
  				c = read();
  			}
  			int res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			//log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  		public final long readLong() throws IOException {
  			int c = read();
  			boolean neg=false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d=(char)c;
  			//log("d:"+d);
  			if (d=='-') {
  				neg=true;
  				c = read();
  			}
  			long res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			//log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  
  
  
  		private boolean isSpaceChar(int c) {
  			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  		}
  	}
  
  
  }
  
  
  