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 Scala (2.11.7) (judge verdict: Accepted)

import java.io.{BufferedReader, InputStreamReader}

import scala.collection.mutable

object Main {
  val MODULO = 998244353L
  val powMemo = new mutable.HashMap[(Long, Long), Long]

  def pow(a: Int, b: Long): Long = {
    if (a == 0) {
      0
    } else if (a == 1) {
      1
    } else {
      powMemo.getOrElseUpdate((a, b), {
        if (b == 0) {
          1
        } else if (b % 2 == 0) {
          pow(a, b / 2) * pow(a, b / 2) % MODULO
        } else {
          pow(a, b - 1) * a % MODULO
        }
      })
    }
  }

  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val n = in.readLine().toInt
    val ds = in.readLine().split("\\s+").map(_.toInt)

    if (ds(0) != 0) {
      System.out.println("0")
      return
    }

    var dsList = ds.sorted.toList
    var result = 0L

    if (dsList.head == 0 && (dsList.tail == Nil || dsList.tail.head == 1)) {
      result = 1L
      var lastElementCount = 1

      dsList = dsList.tail
      for (i <- 1 until n if dsList.nonEmpty) {
        dsList.span(_ == i) match {
          case (as, bs) =>
            result = result * pow(lastElementCount, as.length) % MODULO
            lastElementCount = as.length
            dsList = bs
        }
      }
    } else {
      result = 0L
    }

    System.out.println(
      result
    )
  }
}