Problem:
Score : 300 points

Problem Statement

Given is an integer sequence D_1,...,D_N of N elements. Find the number, modulo 998244353, of trees with N vertices numbered 1 to N that satisfy the following condition:

For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is D_i.

Notes

A tree of N vertices is a connected undirected graph with N vertices and N-1 edges, and the distance between two vertices are the number of edges in the shortest path between them.

Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.

Constraints

1 \leq N \leq 10^5

0 \leq D_i \leq N-1

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the answer.

Sample Input 1

4
0 1 1 2

Sample Output 1

2

For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.

Sample Input 2

4
1 1 1 1

Sample Output 2

0

Sample Input 3

7
0 3 2 1 2 2 1

Sample Output 3

24

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1e5;
int n,m;
set<pair<pair<int,int>,int>>st;
struct node{
	int u;
	long long val;
	long long lazy;
	node(){u = 0;val = 0;lazy = 1e18;}
}seg[4*N];
void fix(int c,int l,int r){
	seg[c].val = min(seg[c].val,seg[c].lazy);
	if(l != r){
		seg[2*c + 1].lazy = min(seg[2*c + 1].lazy,seg[c].lazy);
		seg[2*c + 2].lazy = min(seg[2*c + 2].lazy,seg[c].lazy);
	}
	seg[c].lazy = 1e18;
}
pair<long long,int> get(int c,int l,int r,int s,int e
	){
	fix(c,l,r);
	if(e < l || r < s)return {1e18,-1};
	if(s <= l && r <= e)return {seg[c].val,seg[c].u};
	pair<long long,int> x = get(2*c + 1,l,(l + r)/2,s,e);
	pair<long long,int> y = get(2*c + 2,(l + r)/2 + 1,r,s,e);
	if(x.first == -1)x.first = 1e18;
	if(y.first == -1)y.first = 1e18;
	return min(x,y);
}
void update(int c,int l,int r,int s,int e,long long val){
	fix(c,l,r);
	if(e < l || r < s)return;
	if(s <= l && r <= e){
		seg[c].lazy = val;
		fix(c,l,r);
		return;
	}
	update(2*c + 1,l,(l + r)/2,s,e,val);
	update(2*c + 2,(l + r)/2 + 1,r,s,e,val);
	pair<long long,int> x = {seg[2*c + 1].val,seg[2*c + 1].u};
	pair<long long,int> y = {seg[2*c + 2].val,seg[2*c + 2].u};
	if(x.first == -1)x.first = 1e18;
	if(y.first == -1)y.first = 1e18;
	if(x.first < y.first){
		seg[c].val = x.first;
		seg[c].u = x.second;	
	}
	else{
		seg[c].val = y.first;
		seg[c].u = y.second;
	}
}
void print(int c,int l,int r){
	cout << "from " << l << " to " << r << endl;
	cout << seg[c].u << " " << seg[c].val << endl;
	if(l == r)return;
	print(2*c + 1,l,(l + r)/2);
	print(2*c + 2,(l + r)/2 + 1,r);
}
void dijk(){
	while(1){
		pair<long long,int>temp = get(0,0,n - 1,0,n - 1);
		long long dis = temp.first;
		int u = temp.second;
		if(dis == 1e18)return;
		if(u == n - 1){
			cout << dis << endl;
			exit(0);
		}
		int lol = 0;
		while(st.size()){
			auto it = st.lower_bound({{u,-1},-1});
			if(it == st.end())break;
			if(it->first.second > u)break;
			int l = it->first.second;
			int r = it->first.first;
			int c = it->second;
			update(0,0,n - 1,l,r,dis + c);
			st.erase(it);
		}
		update(0,0,n - 1,u,u,-1);
	}
}
void build(int c,int l,int r){
	if(l == r){
		seg[c].u = l;
		seg[c].val = 1e18;
		if(!l)seg[c].val = 0;
		return;
	}
	build(2*c + 1,l,(l + r)/2);
	build(2*c + 2,(l + r)/2 + 1,r);
	pair<long long,int> x = {seg[2*c + 1].val,seg[2*c + 1].u};
	pair<long long,int> y = {seg[2*c + 2].val,seg[2*c + 1].u};
	if(x.first == -1)x.first = 1e18;
	if(y.first == -1)y.first = 1e18;
	if(x.first < y.first){
		seg[c].val = x.first;
		seg[c].u = x.second;	
	}
	else{
		seg[c].val = y.first;
		seg[c].u = y.second;
	}
}
int main(){
	//freopen("input.txt","r",stdin);
	scanf("%d%d",&n,&m);
	build(0,0,n - 1);
	for(int i = 0,l,r,c;i < m;i++){
		scanf("%d%d%d",&l,&r,&c);
		st.insert({{r - 1,l - 1},c});
	}
	dijk();
	puts("-1");
}