package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

const (
	maxn = int(1e5 + 50)
	mod  = int(998244353)
	eps  = 1e-8
	inf  = int(0x3f3f3f3f)
)

var n, k int
var p []int

func main() {
	scanInts(&n, &k)
	p = scanIntSlice(n)
	flag := -1
	lim := n - k
	res := int64(0)
	res2 := int64(1)
	for i := range p {
		if lim < p[i] {
			res += int64(p[i])
			if flag != -1 {
				MUL(&res2, int64(i - flag))
			}
			flag = i
		}
	}
	printlnInt64s(res, res2)
	close(channelType)
	oWg.Wait()
	out.Flush()
}

/*********************** I/O ***********************/

var channelType chan string
var in *bufio.Scanner
var out = bufio.NewWriter(os.Stdout)
var oWg *sync.WaitGroup

// string and slice are the return value
func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)
	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}
func scanString() string { in.Scan(); return in.Text() }
func scanInt(a *int)     { *a, _ = strconv.Atoi(scanString()) }
func scanInts(a ...*int) {
	for i := range a {
		*a[i], _ = strconv.Atoi(scanString())
	}
}
func scanInt64(a *int64) { *a, _ = strconv.ParseInt(scanString(), 10, 64) }
func scanInt64s(a ...*int64) {
	for i := range a {
		*a[i], _ = strconv.ParseInt(scanString(), 10, 64)
	}
}
func scanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = scanString()
	}
	return s
}
func scanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanInt(&arr[i])
	}
	return arr
}
func scanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		scanInt64(&arr[i])
	}
	return arr
}
func printlnInts(args ...int) {
	for _, x := range args {
		fmt.Fprint(out, x, " ")
	}
	fmt.Fprintln(out)
}
func printlnInt64s(args ...int64) {
	for _, x := range args {
		fmt.Fprint(out, x, " ")
	}
	fmt.Fprintln(out)
}
func printIntSlice(a []int, flag bool) {
	for _, x := range a {
		fmt.Fprint(out, x, " ")
	}
	if flag { fmt.Fprintln(out) }
}
func printInt64Slice(a []int64, flag bool) {
	for _, x := range a {
		fmt.Fprint(out, x, " ")
	}
	if flag { fmt.Fprintln(out) }
}
func printRuneSlice(a []rune, flag bool) {
	for _, x := range a {
		fmt.Fprintf(out, "%c", x)
	}
	if flag { fmt.Fprintln(out) }
}
func putchar(a ...rune) {
	for _, x := range a {
		fmt.Fprintf(out, "%c", x)
	}
}
func printString(s string) { fmt.Fprintln(out, s) }

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string { return fmt.Sprintf("%v", v) }
func spaceSeperatedStringFromArray(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func spaceSeperatedStringFromArray64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func minMaxFromArray(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64         { return a / gcdCalculation(a, b) * b }
func gcdCalculation(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return gcdCalculation(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func absoluteValue(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) {
	minVal, maxVal := int(math.MaxInt32), int(math.MinInt32)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func same(x, y double) bool { return math.Abs(float64(x-y)) < eps }
func popcount(x int) (res int) {
	for i := 0; i < 32; i++ {
		if 1 == (1 & (x >> uint(i))) {
			res++
		}
	}
	return
}
func qp(a, n int64) (res int64) {
	for ; n > 0; n >>= 1 {
		if (n & 1) == 1 {
			res = res * a % int64(mod)
		}
		a = a * a % int64(mod)
	}
	return
}
func divUp(a, b int64) int64 {
	if a % b == 0 { return a / b } else { return a / b + 1 }
}
// [l, r)
func reverse(a *[]rune, l, r int) {
	for i, j := l, r - 1; i < j; i, j = i + 1, j - 1 {
		(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
	}
}
func ADD(a *int64, b int64) { *a = (*a + b) % int64(mod) }
func MUL(a *int64, b int64) { *a = (*a * b) % int64(mod) }

/*********************** Structs ***********************/
type pair struct{ X, Y int }
type double float64
type pairSlice []pair

func (p pairSlice) Len() int { return len(p) }
func (p pairSlice) Less(i, j int) bool {
	if p[i].X != p[j].X {
		return p[i].X < p[j].X
	} else {
		return p[i].Y < p[j].Y
	}
}
func (p pairSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

package main

import (
	"bufio"
	"fmt"
	"os"
)

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	mod := int64(998244353)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	sum := int64(0)
	for i := 0; i < m; i++ {
		sum += int64(n - i)
	}
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	lastPos := -1
	ans := int64(0)
	for i := 0; i < n; i++ {
		if a[i]+(m-1) >= n {
			if lastPos == -1 {
				ans = 1
				lastPos = i
			} else {
				ans *= int64(i - lastPos)
				ans %= mod
				lastPos = i
			}
		}
	}
	fmt.Fprintf(out, "%d %d\n", sum, ans)
	return
}

func test(a []int, b [][]int) []int {
	fmt.Println("debug")
	fmt.Println(a)
	for i := 0; i < len(b); i++ {
		a[b[i][0]-1], a[b[i][1]-1] = a[b[i][1]-1], a[b[i][0]-1]
		fmt.Println(a, b[i])
	}
	fmt.Println("debug end")

	return a
}

// find num of elem >= k
func find(a []int, k int) int {
	if len(a) == 0 {
		return 0
	}
	n := len(a)
	if a[0] > k {
		return n
	}
	if a[n-1] < k {
		return 0
	}
	l := 0
	r := len(a) - 1
	for l < r {
		mid := (l + r) / 2
		if a[mid] < k {
			l = mid + 1
		} else {
			r = mid
		}
	}
	return n - l
}

func gcd(a, b uint64) uint64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func ok(a, b int) bool {
	if min(abs(a-b), abs(a+b)) <= min(abs(a), abs(b)) && max(abs(a+b), abs(a-b)) >= max(abs(a), abs(b)) {
		return true
	}
	return false
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, k := NextInt(), NextInt()
	lastPos := -1
	var res int64
	var positions int64
	positions = 1
	var MOD int64
	MOD = 998244353
	for i := 0; i < n; i++ {
		pi := NextInt()
		if pi > n-k {
			res += int64(pi)
			if lastPos != -1 {
				positions *= int64(i - lastPos)
				positions %= MOD
			}
			lastPos = i
		}
	}
	fmt.Fprintln(out, res, positions)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	k := in()

	p := -1

	a := make([]int, n)

	for i := range a {
		a[i] = in()
	}

	var res int64 = 1
	var sum int64 = 0

	for i := range a {
		if a[i] > n-k {
			sum += int64(a[i])
			if p != -1 {
				res *= int64(i - p)
				res %= 998244353
			}
			p = i
		}
	}

	fmt.Println(sum, res)

}

/*
URL:
https://codeforces.com/problemset/problem/1326/C
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n, k int64
	P    []int64
)

func main() {
	n, k = ReadInt64_2()
	P = ReadInt64Slice(int(n))

	// cf := NewCombFactorial(500000)
	// num := cf.C(n-1, k-1)

	ans := int64(0)
	for i := int64(0); i < k; i++ {
		ans += n - i
	}

	memo := []int64{}
	for i := int64(0); i < n; i++ {
		if P[i] > n-k {
			memo = append(memo, i)
		}
	}
	num := int64(1)
	for i := 1; i < len(memo); i++ {
		diff := int64(memo[i] - memo[i-1])
		num *= diff
		num %= MOD
	}

	fmt.Println(ans, num)
}

// cf := NewCombFactorial(500000) // maxNum == "maximum n" * 2 (for H(n,r))
// res := cf.C(n, r) 	// 組み合わせ
// res := cf.H(n, r) 	// 重複組合せ
// res := cf.P(n, r) 	// 順列

type CombFactorial struct {
	factorial, invFactorial []int64
	maxNum                  int64
}

func NewCombFactorial(maxNum int64) *CombFactorial {
	cf := new(CombFactorial)
	cf.maxNum = maxNum
	cf.factorial = make([]int64, maxNum+50)
	cf.invFactorial = make([]int64, maxNum+50)
	cf.initCF()

	return cf
}
func (c *CombFactorial) modInv(a int64) int64 {
	return c.modpow(a, MOD-2)
}
func (c *CombFactorial) modpow(a, e int64) int64 {
	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := c.modpow(a, halfE)
		return half * half % MOD
	}

	return a * c.modpow(a, e-1) % MOD
}
func (c *CombFactorial) initCF() {
	var i int64

	for i = 0; i <= c.maxNum; i++ {
		if i == 0 {
			c.factorial[i] = 1
			c.invFactorial[i] = c.modInv(c.factorial[i])
			continue
		}

		num := i * c.factorial[i-1]
		num %= MOD
		c.factorial[i] = num
		c.invFactorial[i] = c.modInv(c.factorial[i])
	}
}
func (c *CombFactorial) C(n, r int64) int64 {
	var res int64

	res = 1
	res *= c.factorial[n]
	res %= MOD
	res *= c.invFactorial[r]
	res %= MOD
	res *= c.invFactorial[n-r]
	res %= MOD

	return res
}
func (c *CombFactorial) P(n, r int64) int64 {
	var res int64

	res = 1
	res *= c.factorial[n]
	res %= MOD
	res *= c.invFactorial[n-r]
	res %= MOD

	return res
}
func (c *CombFactorial) H(n, r int64) int64 {
	return c.C(n-1+r, r)
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1326C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, k, v int
	Fscan(in, &n, &k)
	pos := make([]int, 0, k)
	for i := 0; i < n; i++ {
		if Fscan(in, &v); v > n-k {
			pos = append(pos, i)
		}
	}
	ans := int64(1)
	for i := 1; i < k; i++ {
		ans = ans * int64(pos[i]-pos[i-1]) % 998244353
	}
	Fprintln(out, int64(2*n-k+1)*int64(k)/2, ans)
}

func main() { CF1326C(os.Stdin, os.Stdout) }

package main

import (
	"fmt"
	"os"
	"bufio"
	"sort"
)

const SZ int = 2e5 + 5
const MOD int64 = 998244353

type mypair struct {
	x, loc int
}

type mypairslice []mypair

func (m mypairslice) Len() int { return len(m)}
func (m mypairslice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }

type SortByX struct{ mypairslice }
func (sbx SortByX) Less(i,j int) bool { return sbx.mypairslice[i].x > sbx.mypairslice[j].x }

type SortByLoc struct { mypairslice }
func (sbl SortByLoc) Less(i,j int) bool { return sbl.mypairslice[i].loc > sbl.mypairslice[j].loc }

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	var n, k int
	scanf("%d%d\n", &n, &k)
	var p mypairslice = make(mypairslice, n)
	for i:= 0; i < n; i++ {
		scanf("%d", &p[i].x)
		p[i].loc = i
		//fmt.Printf("p[%d]=%d %d\n", i, p[i].x, p[i].loc)
	}
	sort.Sort(SortByX{p})
	// for i:=0; i < n; i++ {
	// 	fmt.Printf("p[%d]=%d %d\n", i, p[i].x, p[i].loc)
	// }
	var pk mypairslice = p[:k]
	sort.Sort(sort.Reverse(SortByLoc{pk}))
	// for i:=0; i < k; i++ {
	// 	fmt.Printf("pk[%d]=%d %d\n", i, pk[i].x, pk[i].loc)
	// }
	var sum, count int64 = 0, 1
	sum += int64(pk[0].x)
	for i:= 1; i < k; i++ {
		sum += int64(pk[i].x)
		count *= int64(pk[i].loc - pk[i-1].loc)
		count %= MOD
	}
	fmt.Printf("%d %d\n", sum, count)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()
	p := sc.NextIntArray()

	a := make([]int, N)
	copy(a, p)
	sort.Ints(a)

	best := a[len(a)-K]
	first := int64(0)

	pre := -1
	ans := int64(1)
	for i := 0; i < N; i++ {
		if p[i] >= best {
			first += int64(p[i])
			if pre >= 0 {
				ans *= int64(i - pre)
			}
			ans %= 998244353
			pre = i
		}
	}
	fmt.Println(first, ans)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

const MOD = 998244353

func main() {
	defer writer.Flush()
	n := reader.NextInt64()
	k := reader.NextInt64()
	pos := []int64{}
	val := int64(0)
	for i := int64(0); i < n; i++ {
		p := reader.NextInt64()
		if p > n-k {
			pos = append(pos, i)
			val += p
		}
	}
	ret := int64(1)
	for i := 1; i < len(pos); i++ {
		ret = (ret * (pos[i] - pos[i-1])) % MOD
	}
	writer.Printf("%d %d", val, ret)
}

func sum(ii []int64) int64 {
	ret := int64(0)
	for _, i := range ii {
		ret += i
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func fillNNums(bs []byte, n int, res []int) {
	x := 0
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	n, k := readTwoNums(reader)

	P := readNNums(reader, n)

	a, b := solve(n, k, P)
	fmt.Printf("%d %d\n", a, b)
}

const MOD = 998244353

func solve(n, k int, P []int) (int64, int64) {
	var K int64

	for i := 0; i < k; i++ {
		K += int64(n - i)
	}

	var res int64 = 1

	var left int
	for P[left] <= n-k {
		left++
	}
	// j is the first one

	for i := left + 1; i < n; i++ {
		if P[i] <= n-k {
			continue
		}
		// P[i] > n - k
		cnt := int64(i - left)

		res *= cnt
		res %= MOD

		left = i
	}

	return K, res
}

package main

import (
	"fmt"
	"os"
	"bufio"
	"sort"
)

const SZ int = 2e5 + 5
const MOD int = 998244353

type mypair struct {
	x, loc int
}

type mypairslice []mypair

func (m mypairslice) Len() int { return len(m)}
func (m mypairslice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }

type SortByX struct{ mypairslice }
func (sbx SortByX) Less(i,j int) bool { return sbx.mypairslice[i].x > sbx.mypairslice[j].x }

type SortByLoc struct { mypairslice }
func (sbl SortByLoc) Less(i,j int) bool { return sbl.mypairslice[i].loc > sbl.mypairslice[j].loc }

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	var n, k int
	scanf("%d%d\n", &n, &k)
	var p mypairslice = make(mypairslice, n)
	for i:= 0; i < n; i++ {
		scanf("%d", &p[i].x)
		p[i].loc = i
		//fmt.Printf("p[%d]=%d %d\n", i, p[i].x, p[i].loc)
	}
	sort.Sort(SortByX{p})
	// for i:=0; i < n; i++ {
	// 	fmt.Printf("p[%d]=%d %d\n", i, p[i].x, p[i].loc)
	// }
	var pk mypairslice = p[:k]
	sort.Sort(sort.Reverse(SortByLoc{pk}))
	// for i:=0; i < k; i++ {
	// 	fmt.Printf("pk[%d]=%d %d\n", i, pk[i].x, pk[i].loc)
	// }
	var sum, count int = 0, 1
	sum += pk[0].x
	for i:= 1; i < k; i++ {
		sum += pk[i].x
		count *= pk[i].loc - pk[i-1].loc
		count %= MOD
	}
	fmt.Printf("%d %d\n", sum, count)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()
	p := sc.NextIntArray()

	a := make([]int, N)
	copy(a, p)
	sort.Ints(a)

	best := a[len(a)-K]
	first := 0

	pre := -1
	ans := int64(1)
	for i := 0; i < N; i++ {
		if p[i] >= best {
			first += p[i]
			if pre >= 0 {
				ans *= int64(i - pre)
			}
			ans %= 998244353
			pre = i
		}
	}
	fmt.Println(first, ans)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()
	p := sc.NextIntArray()

	a := make([]int, N)
	copy(a, p)
	sort.Ints(a)

	best := a[len(a)-K]
	first := 0

	pre := -1
	ans := 1
	for i := 0; i < N; i++ {
		if p[i] >= best {
			first += p[i]
			if pre >= 0 {
				ans *= i - pre
			}
			ans %= 998244353
			pre = i
		}
	}
	fmt.Println(first, ans)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	mod := int64(998244353)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	sum := 0
	for i := 0; i < m; i++ {
		sum += n - i
	}
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	lastPos := -1
	ans := int64(0)
	for i := 0; i < n; i++ {
		if a[i]+(m-1) >= n {
			if lastPos == -1 {
				ans = 1
				lastPos = i
			} else {
				ans *= int64(i - lastPos)
				ans %= mod
				lastPos = i
			}
		}
	}
	fmt.Fprintf(out, "%d %d\n", sum, ans)
	return
}

func test(a []int, b [][]int) []int {
	fmt.Println("debug")
	fmt.Println(a)
	for i := 0; i < len(b); i++ {
		a[b[i][0]-1], a[b[i][1]-1] = a[b[i][1]-1], a[b[i][0]-1]
		fmt.Println(a, b[i])
	}
	fmt.Println("debug end")

	return a
}

// find num of elem >= k
func find(a []int, k int) int {
	if len(a) == 0 {
		return 0
	}
	n := len(a)
	if a[0] > k {
		return n
	}
	if a[n-1] < k {
		return 0
	}
	l := 0
	r := len(a) - 1
	for l < r {
		mid := (l + r) / 2
		if a[mid] < k {
			l = mid + 1
		} else {
			r = mid
		}
	}
	return n - l
}

func gcd(a, b uint64) uint64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func ok(a, b int) bool {
	if min(abs(a-b), abs(a+b)) <= min(abs(a), abs(b)) && max(abs(a+b), abs(a-b)) >= max(abs(a), abs(b)) {
		return true
	}
	return false
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, k := NextInt(), NextInt()
	lastPos := -1
	res := 0
	var positions int64
	positions = 1
	var MOD int64
	MOD = 998244353
	for i := 0; i < n; i++ {
		pi := NextInt()
		if pi > n-k {
			res += pi
			if lastPos != -1 {
				positions *= int64(i - lastPos)
				positions %= MOD
			}
			lastPos = i
		}
	}
	fmt.Fprintln(out, res, positions)
}


// Author: sighduck
// URL: https://codeforces.com/problemset/problem/763/A

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Edge struct {
	from int
	to   int
}

type QueueItem struct {
	color  int
	vertex int
}

func search(n int, graph [][]int, colors []int, start int) bool {
	queue := []QueueItem{}
	visited := make([]bool, n+1)
	for _, vertex := range graph[start] {
		queue = append(queue, QueueItem{colors[vertex-1], vertex})
		visited[vertex] = true
	}
	for len(queue) > 0 {
		current := queue[0]
		queue = queue[1:]
		if current.color != colors[current.vertex-1] {
			return false
		}
		for _, vertex := range graph[current.vertex] {
			if vertex != start && !visited[vertex] {
				queue = append(queue, QueueItem{current.color, vertex})
				visited[vertex] = true
			}
		}
	}
	return true
}

func Solve(n int, edges []Edge, colors []int) int {
	graph := make([][]int, n+1)
	for _, edge := range edges {
		graph[edge.from] = append(graph[edge.from], edge.to)
		graph[edge.to] = append(graph[edge.to], edge.from)
	}
	for _, edge := range edges {
		if colors[edge.from-1] != colors[edge.to-1] {
			if search(n, graph, colors, edge.from) {
				return edge.from
			} else if search(n, graph, colors, edge.to) {
				return edge.to
			} else {
				return -1
			}
		}
	}
	return 1
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	edges := make([]Edge, n-1)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}
	colors := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &colors[i])
	}

	answer := Solve(n, edges, colors)
	if answer != -1 {
		fmt.Fprintf(writer, "YES\n%d\n", answer)
	} else {
		fmt.Fprintf(writer, "NO\n")
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type UnionFind struct {
	ranks   []int
	parents []int
	counts  []int
}

func NewUnionFind(sz int) *UnionFind {
	ra := make([]int, sz)
	pa := make([]int, sz)
	co := make([]int, sz)
	for i := 0; i < sz; i++ {
		ra[i] = 1
		co[i] = 1
		pa[i] = i
	}
	uf := UnionFind{
		ranks:   ra,
		parents: pa,
		counts:  co,
	}
	return &uf
}

func (this *UnionFind) FindParent(x int) int {
	if x == this.parents[x] {
		return x
	} else {
		this.parents[x] = this.FindParent(this.parents[x])
		return this.parents[x]
	}
}
func (this *UnionFind) IsSame(x int, y int) bool {
	return this.FindParent(x) == this.FindParent(y)
}
func (this *UnionFind) Unite(x int, y int) {
	px := this.FindParent(x)
	py := this.FindParent(y)
	if px == py {
		return
	}
	if this.ranks[px] > this.ranks[py] {
		this.parents[py] = px
		this.counts[px] += this.counts[py]
	} else {
		this.parents[px] = py
		this.counts[py] += this.counts[px]
		if this.ranks[px] == this.ranks[py] {
			this.ranks[py] += 1
		}
	}
}
func (this *UnionFind) GroupSize(x int) int {
	return this.counts[this.FindParent(x)]
}

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	sc.Split(bufio.ScanWords)
	var n int
	fmt.Scanf("%d", &n)

	uf := NewUnionFind(n + 1)
	es := make(map[int][]int)

	for i := 0; i < n-1; i++ {
		u := nextInt()
		v := nextInt()
		es[u] = append(es[u], v)
		es[v] = append(es[v], u)
	}

	cs := make([]int, n+1)
	for i := 1; i <= n; i++ {
		c := nextInt()
		cs[i] = c
	}

	for u, vs := range es {
		for _, v := range vs {
			if cs[u] == cs[v] {
				uf.Unite(u, v)
			}
		}
	}

	st := make(map[int]int)
	for i := 1; i <= n; i++ {
		st[uf.FindParent(i)] = 1
	}

	target_v := -1
	for u, vs := range es {
		tmp := make(map[int]int)
		for _, v := range vs {
			tmp[uf.FindParent(v)] = 1
		}
		x := 0
		if uf.GroupSize(u) == 1 {
			x = 1
		}
		if len(st)-x == len(tmp) {
			target_v = u
			break
		}
	}

	if target_v != -1 {
		fmt.Printf("YES\n%d\n", target_v)
	} else {
		fmt.Printf("NO\n")
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([][2]Int, n)
	for i := Int(0); i < n-1; i++ {
		I(&in[i][0], &in[i][1])
	}
	c := make([]Int, n+1)
	for i := Int(1); i < n+1; i++ {
		I(&c[i])
	}
	cc := make([]Int, n+1)
	var ccc, m Int
	for i := Int(0); i < n-1; i++ {
		if c[in[i][0]] != c[in[i][1]] {
			cc[in[i][0]]++
			cc[in[i][1]]++
			m = in[i][0]
			if cc[m] < cc[in[i][1]] {
				m = in[i][1]
			}
			ccc++
			if cc[m] != ccc {
				O("NO\n")
				return
			}
		}
	}
	if m == 0 {
		m++
	}
	O("YES\n", m, "\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([][2]Int, n)
	for i := Int(0); i < n-1; i++ {
		I(&in[i][0], &in[i][1])
	}
	c := make([]Int, n+1)
	for i := Int(1); i < n+1; i++ {
		I(&c[i])
	}
	cc := make([]Int, n+1)
	var ccc Int
	for i := Int(0); i < n-1; i++ {
		if c[in[i][0]] != c[in[i][1]] {
			cc[in[i][0]]++
			cc[in[i][1]]++
			ccc++
		}
	}
	for i := Int(1); i < n+1; i++ {
		if ccc == cc[i] {
			O("YES\n", i, "\n")
			return
		}
	}
	O("NO\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n    int
	g    [][]int
	c    []int
	memo map[int64]bool
)

func solve() {
	memo = make(map[int64]bool)
	n = readInt()
	g = make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		u := readInt() - 1
		v := readInt() - 1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	c = make([]int, n)
	for i := 0; i < n; i++ {
		c[i] = readInt()
	}
	for i := 0; i < n; i++ {
		ok := true
		for _, v := range g[i] {
			if !dfs(v, i, c[v]) {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
			fmt.Println(i + 1)
			return
		}
	}
	fmt.Println("NO")
}

func dfs(v int, from int, color int) bool {
	if c[v] != color {
		return false
	}
	key := int64(v)*1<<32 + int64(from)
	if v, ok := memo[key]; ok {
		return v
	}
	res := true
	for _, u := range g[v] {
		if u != from {
			if !dfs(u, v, color) {
				res = false
				break
			}
		}
	}
	memo[key] = res
	return res
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main;
import ("fmt"
        "bufio"
        "os"
        )
const (
    MAX_N=1e5+1;
)
type subTree struct{
    valid bool;
    length int;
    color int;//0 neutral -1 invalid
}
func sum(a subTree,b subTree) subTree{
    if a.color==0 {
        return b;
    }
    if b.color==0 {
        return a;
    }
    if a.valid&&b.valid&&a.color==b.color{
        return subTree{true,a.length+b.length,a.color};
    }
    return subTree{false,0,-1};
}
var G [MAX_N][]int;
var col [MAX_N]int;
var T [MAX_N]subTree;
var F [MAX_N]int;
var cnt int;
func update(color int,x int,sign int){
    if color<=0{
        return;
    }
    if sign==1{
        if F[color]==0{
            cnt++;
        }
    }else{
        if x>0&&F[color]==x{
            cnt--;
        }
    }
    F[color]+=sign*x;
    if F[color]<0{
        panic("WTF");
    }
}
func initGraph(n int){
    for i:=0;i<n;i++{
        G[i]=G[i][:0];
    }
}
func add(u int,v int){
    G[u]=append(G[u],v);
    G[v]=append(G[v],u);
}
var ans bool;
var id int;
func dfs(u int,p int){
    //fmt.Println(u+1,F);
    T[u]=subTree{true,0,0};
    for _,v:=range G[u]{
        if v==p{
            continue;
        }

        dfs(v,u);
        T[u]=sum(T[u],T[v]);
    }
    allValid:=true;
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,-1);
        }else{
            allValid=false;
        }
    }
    update(col[u],1,-1);
    //fmt.Println(u,cnt,F,T[u]);
    if cnt<=1&&allValid {
        ans=true;
        id=u;
    }
    update(col[u],1,+1);
    T[u]=sum(T[u],subTree{true,1,col[u]});
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,+1);
        }
    }
}
func main(){
    in := bufio.NewReader(os.Stdin);
    var n int;
    for true{
        if items,_:=fmt.Fscanf(in, "%d\n", &n);items!=1{
            break;
        }
        ans=false;
        initGraph(n);
        cnt=0;
        for i:=0;i<MAX_N;i++{
            F[i]=0;
        }
        for i:=0;i<n-1;i++{
            var u,v int;
            fmt.Fscanf(in, "%d %d\n", &u, &v);
            u--;v--;
            add(u,v);
        }
        for i:=0;i<n;i++{
            var x int;
            fmt.Fscanf(in, "%d\n", &x);
            col[i]=x;
            update(x,1,+1);
        }
        //fmt.Println(F);
        dfs(0,-1);
        if ans{
            fmt.Println("YES");
            fmt.Println(id+1);
        }else{
            fmt.Println("NO");
        }
    }
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func Solve(io *FastIO) {
	N := io.NextInt()

	nodes := make([]Node, N + 1)
	for i := 1; i < N; i++ {
		U := io.NextInt()
		V := io.NextInt()

		nodes[U].next = append(nodes[U].next, &nodes[V])
		nodes[V].next = append(nodes[V].next, &nodes[U])
	}

	for i := 1; i <= N; i++ {
		C := io.NextInt()

		nodes[i].color = C
		nodes[i].group = -1
	}

	grp := 0
	for i := 1; i <= N; i++ {
		if nodes[i].group < 0 {
			FillGroup(&nodes[i], grp)
			grp++
		}
	}

	for i := 1; i <= N; i++ {
		set := make(map[int]bool)
		set[nodes[i].group] = true
		for _, n := range nodes[i].next {
			set[n.group] = true
		}
		if len(set) == grp {
			io.Println("YES")
			io.Println(i)
			return
		}
	}
	io.Println("NO")
}

type Node struct {
	next []*Node
	color int
	group int
}

func FillGroup(node *Node, grp int) {
	node.group = grp
	for _, n := range node.next {
		if n.color == node.color && n.group < 0 {
			FillGroup(n, grp)
		}
	}
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Solve(&io)
	io.FlushOutput()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type UnionFind struct {
	ranks   []int
	parents []int
	counts  []int
}

func NewUnionFind(sz int) *UnionFind {
	ra := make([]int, sz)
	pa := make([]int, sz)
	co := make([]int, sz)
	for i := 0; i < sz; i++ {
		ra[i] = 1
		co[i] = 1
		pa[i] = i
	}
	uf := UnionFind{
		ranks:   ra,
		parents: pa,
		counts:  co,
	}
	return &uf
}

func (this *UnionFind) FindParent(x int) int {
	if x == this.parents[x] {
		return x
	} else {
		this.parents[x] = this.FindParent(this.parents[x])
		return this.parents[x]
	}
}
func (this *UnionFind) IsSame(x int, y int) bool {
	return this.FindParent(x) == this.FindParent(y)
}
func (this *UnionFind) Unite(x int, y int) {
	px := this.FindParent(x)
	py := this.FindParent(y)
	if px == py {
		return
	}
	if this.ranks[px] > this.ranks[py] {
		this.parents[py] = px
		this.counts[px] += this.counts[py]
	} else {
		this.parents[px] = py
		this.counts[py] += this.counts[px]
		if this.ranks[px] == this.ranks[py] {
			this.ranks[py] += 1
		}
	}
}
func (this *UnionFind) GroupSize(x int) int {
	return this.counts[this.FindParent(x)]
}

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	sc.Split(bufio.ScanWords)
	var n int
	fmt.Scanf("%d", &n)

	uf := NewUnionFind(n + 1)
	es := make(map[int][]int)

	for i := 0; i < n-1; i++ {
		u := nextInt()
		v := nextInt()
		es[u] = append(es[u], v)
		es[v] = append(es[v], u)
	}

	cs := make([]int, n+1)
	for i := 1; i <= n; i++ {
		c := nextInt()
		cs[i] = c
	}

	for u, vs := range es {
		for _, v := range vs {
			if cs[u] == cs[v] {
				uf.Unite(u, v)
			}
		}
	}

	st := make(map[int]int)
	for i := 1; i <= n; i++ {
		st[uf.FindParent(i)] = 1
	}

	target_v := -1
	for u, vs := range es {
		tmp := make(map[int]int)
		for v := range vs {
			tmp[uf.FindParent(v)] = 1
		}
		x := 0
		if uf.GroupSize(u) == 1 {
			x = 1
		}
		if len(st)-x == len(tmp) {
			target_v = u
			break
		}
	}

	if target_v != -1 {
		fmt.Printf("YES\n%d\n", target_v)
	} else {
		fmt.Printf("NO\n")
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([][2]Int, n)
	for i := Int(0); i < n-1; i++ {
		I(&in[i][0], &in[i][1])
	}
	c := make([]Int, n+1)
	for i := Int(1); i < n+1; i++ {
		I(&c[i])
	}
	cc := make([]Int, n+1)
	var ccc, m Int
	for i := Int(0); i < n-1; i++ {
		if c[in[i][0]] != c[in[i][1]] {
			cc[in[i][0]]++
			cc[in[i][1]]++
			m = in[i][0]
			if m < cc[in[i][1]] {
				m = in[i][1]
			}
			ccc++
			if cc[m] != ccc {
				O("NO\n")
				return
			}
		}
	}
	if m == 0 {
		m++
	}
	O("YES\n", m, "\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([][2]Int, n)
	for i := Int(0); i < n-1; i++ {
		I(&in[i][0], &in[i][1])
	}
	c := make([]Int, n+1)
	for i := Int(1); i < n+1; i++ {
		I(&c[i])
	}
	cc := make([]Int, n+1)
	var ccc, m Int
	for i := Int(0); i < n-1; i++ {
		if c[in[i][0]] != c[in[i][1]] {
			cc[in[i][0]]++
			cc[in[i][1]]++
			m = in[i][0]
			if m < cc[in[i][1]] {
				m = in[i][1]
			}
			ccc++
		}
	}
	if m == 0 {
		m++
	} else if cc[m] != ccc {
		O("NO\n")
		return
	}
	O("YES\n", m, "\n")
}

package main;
import ("fmt"
        "bufio"
        "os"
        )
const (
    MAX_N=1e5+1;
)
type subTree struct{
    valid bool;
    length int;
    color int;//0 neutral -1 invalid
}
func sum(a subTree,b subTree) subTree{
    if a.color==0 {
        return b;
    }
    if b.color==0 {
        return a;
    }
    if a.valid&&b.valid&&a.color==b.color{
        return subTree{true,a.length+b.length,a.color};
    }
    return subTree{false,0,-1};
}
var G [MAX_N][]int;
var col [MAX_N]int;
var T [MAX_N]subTree;
var F [MAX_N]int;
var cnt int;
func update(color int,x int,sign int){
    if color<=0{
        return;
    }
    if sign==1{
        if F[color]==0{
            cnt++;
        }
    }else{
        if x>0&&F[color]==x{
            cnt--;
        }
    }
    F[color]+=sign*x;
}
func initGraph(n int){
    for i:=0;i<n;i++{
        G[i]=G[i][:0];
    }
}
func add(u int,v int){
    G[u]=append(G[u],v);
    G[v]=append(G[v],u);
}
var ans bool;
var id int;
func dfs(u int,p int){
    T[u]=subTree{true,0,0};
    for _,v:=range G[u]{
        if v==p{
            continue;
        }

        dfs(v,u);
        T[u]=sum(T[u],T[v]);
        update(T[v].color,T[v].length,-1);
    }

    update(col[u],1,-1);
    //fmt.Println(u,cnt,F,T[u]);
    if cnt==1 {
        ans=true;
        id=u;
    }
    update(col[u],1,+1);
    T[u]=sum(T[u],subTree{true,1,col[u]});
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        update(T[v].color,T[v].length,+1);
    }
}
func main(){
    in := bufio.NewReader(os.Stdin);
    var n int;
    for true{
        if items,_:=fmt.Fscanf(in, "%d\n", &n);items!=1{
            break;
        }
        ans=false;
        initGraph(n);
        cnt=0;
        for i:=0;i<MAX_N;i++{
            F[i]=0;
        }
        for i:=0;i<n-1;i++{
            var u,v int;
            fmt.Fscanf(in, "%d %d\n", &u, &v);
            u--;v--;
            add(u,v);
        }
        for i:=0;i<n;i++{
            var x int;
            fmt.Fscanf(in, "%d\n", &x);
            col[i]=x;
            update(x,1,+1);
        }
        dfs(0,-1);
        if ans{
            fmt.Println("YES");
            fmt.Println(id+1);
        }else{
            fmt.Println("NO");
        }
    }
}

package main;
import ("fmt"
        "bufio"
        "os"
        )
const (
    MAX_N=1e5+1;
)
type subTree struct{
    valid bool;
    length int;
    color int;//0 neutral -1 invalid
}
func sum(a subTree,b subTree) subTree{
    if a.color==0 {
        return b;
    }
    if b.color==0 {
        return a;
    }
    if a.valid&&b.valid&&a.color==b.color{
        return subTree{true,a.length+b.length,a.color};
    }
    return subTree{false,0,-1};
}
var G [MAX_N][]int;
var col [MAX_N]int;
var T [MAX_N]subTree;
var F [MAX_N]int;
var cnt int;
func update(color int,x int,sign int){
    if color<=0{
        return;
    }
    if sign==1{
        if F[color]==0{
            cnt++;
        }
    }else{
        if x>0&&F[color]==x{
            cnt--;
        }
    }
    F[color]+=sign*x;
    if F[color]<0{
        panic("WTF");
    }
}
func initGraph(n int){
    for i:=0;i<n;i++{
        G[i]=G[i][:0];
    }
}
func add(u int,v int){
    G[u]=append(G[u],v);
    G[v]=append(G[v],u);
}
var ans bool;
var id int;
func dfs(u int,p int){
    //fmt.Println(u+1,F);
    T[u]=subTree{true,0,0};
    for _,v:=range G[u]{
        if v==p{
            continue;
        }

        dfs(v,u);
        T[u]=sum(T[u],T[v]);
    }
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,-1);
        }
    }
    update(col[u],1,-1);
    //fmt.Println(u,cnt,F,T[u]);
    if cnt<=1&&T[u].valid {
        ans=true;
        id=u;
    }
    update(col[u],1,+1);
    T[u]=sum(T[u],subTree{true,1,col[u]});
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,+1);
        }
    }
}
func main(){
    in := bufio.NewReader(os.Stdin);
    var n int;
    for true{
        if items,_:=fmt.Fscanf(in, "%d\n", &n);items!=1{
            break;
        }
        ans=false;
        initGraph(n);
        cnt=0;
        for i:=0;i<MAX_N;i++{
            F[i]=0;
        }
        for i:=0;i<n-1;i++{
            var u,v int;
            fmt.Fscanf(in, "%d %d\n", &u, &v);
            u--;v--;
            add(u,v);
        }
        for i:=0;i<n;i++{
            var x int;
            fmt.Fscanf(in, "%d\n", &x);
            col[i]=x;
            update(x,1,+1);
        }
        //fmt.Println(F);
        dfs(0,-1);
        if ans{
            fmt.Println("YES");
            fmt.Println(id+1);
        }else{
            fmt.Println("NO");
        }
    }
}

package main;
import ("fmt"
        "bufio"
        "os"
        )
const (
    MAX_N=1e5+1;
)
type subTree struct{
    valid bool;
    length int;
    color int;//0 neutral -1 invalid
}
func sum(a subTree,b subTree) subTree{
    if a.color==0 {
        return b;
    }
    if b.color==0 {
        return a;
    }
    if a.valid&&b.valid&&a.color==b.color{
        return subTree{true,a.length+b.length,a.color};
    }
    return subTree{false,0,-1};
}
var G [MAX_N][]int;
var col [MAX_N]int;
var T [MAX_N]subTree;
var F [MAX_N]int;
var cnt int;
func update(color int,x int,sign int){
    if color<=0{
        return;
    }
    if sign==1{
        if F[color]==0{
            cnt++;
        }
    }else{
        if x>0&&F[color]==x{
            cnt--;
        }
    }
    F[color]+=sign*x;
    if F[color]<0{
        panic("WTF");
    }
}
func initGraph(n int){
    for i:=0;i<n;i++{
        G[i]=G[i][:0];
    }
}
func add(u int,v int){
    G[u]=append(G[u],v);
    G[v]=append(G[v],u);
}
var ans bool;
var id int;
func dfs(u int,p int){
    //fmt.Println(u+1,F);
    T[u]=subTree{true,0,0};
    for _,v:=range G[u]{
        if v==p{
            continue;
        }

        dfs(v,u);
        T[u]=sum(T[u],T[v]);
    }
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,-1);
        }
    }
    update(col[u],1,-1);
    //fmt.Println(u,cnt,F,T[u]);
    if cnt==1&&T[u].valid {
        ans=true;
        id=u;
    }
    update(col[u],1,+1);
    T[u]=sum(T[u],subTree{true,1,col[u]});
    for _,v:=range G[u]{
        if v==p{
            continue;
        }
        if T[v].valid{
            update(T[v].color,T[v].length,+1);
        }
    }
}
func main(){
    in := bufio.NewReader(os.Stdin);
    var n int;
    for true{
        if items,_:=fmt.Fscanf(in, "%d\n", &n);items!=1{
            break;
        }
        ans=false;
        initGraph(n);
        cnt=0;
        for i:=0;i<MAX_N;i++{
            F[i]=0;
        }
        for i:=0;i<n-1;i++{
            var u,v int;
            fmt.Fscanf(in, "%d %d\n", &u, &v);
            u--;v--;
            add(u,v);
        }
        for i:=0;i<n;i++{
            var x int;
            fmt.Fscanf(in, "%d\n", &x);
            col[i]=x;
            update(x,1,+1);
        }
        //fmt.Println(F);
        dfs(0,-1);
        if ans{
            fmt.Println("YES");
            fmt.Println(id+1);
        }else{
            fmt.Println("NO");
        }
    }
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

const N = 3000010
const INF = 1000000001
var n int
var a [N] int
var p [N] int
var pos[N] int

func max(a int, b int) int {
	if a < b {
		return b
	}
	return a
}

type SegNode struct {
	value int
	agg int
	max int
}

var Tree [4 * N] SegNode

func lazy(node int)  {
	if Tree[node].agg > 0 {
		Tree[node * 2].agg += Tree[node].agg
		Tree[node * 2 + 1].agg += Tree[node].agg
		Tree[node].agg = 0
	}
}

func addValue(node int, l int, r int, u int, v int, val int)  {
	if l > v || r < u {
		return
	}
	if l >= u && r <= v {
		Tree[node].agg += val
		return
	}

	var mid = (l + r) >> 1
	lazy(node)
	addValue(node * 2, l, mid, u, v, val)
	addValue(node * 2 + 1, mid + 1, r, u, v, val)
	Tree[node].max = max(Tree[node * 2].max + Tree[node * 2].agg, Tree[node * 2 + 1].max + Tree[node * 2 + 1].agg)
}

func getMax(node int, l int, r int, u int, v int) int {
	if r < u || l > v {
		return -INF
	}
	if l >= u && r <= v {
		return Tree[node].agg + Tree[node].max
	}
	var mid = (l + r) >> 1
	lazy(node)
	var maxValue = getMax(node * 2, l, mid, u, v)
	maxValue = max(maxValue, getMax(node * 2 + 1, mid + 1, r, u, v))
	return maxValue
}

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	fmt.Fscanf(reader, "%d\n", &n)
	for i:=0; i < n; i++ {
		fmt.Fscanf(reader,"%d", &a[i])
		pos[a[i]] = i + 1
	}
	fmt.Fscanf(reader,"\n")

	for i:=0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &p[i])
	}

	var curAns = n
	addValue(1, 1, n, 1, pos[curAns], 1)

	for i := 0; i < n; i++ {

		for true {
			var maxValue = getMax(1, 1, n, 1, n)
			if maxValue > 0 {
				break
			}
			curAns--
			addValue(1, 1, n, 1, pos[curAns],1)
		}

		fmt.Fprintf(writer, "%d ", curAns)
		addValue(1, 1, n, 1, p[i], -1)

	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var seg [2000000]int
var lazy [2000000]int

func segPlus(index, l, r, left, right, plus int) {
	if l == left && r == right {
		lazy[index] += plus
		return
	}
	mid := (l + r) / 2
	if right <= mid {
		segPlus(index*2+1, l, mid, left, right, plus)
	} else if left > mid {
		segPlus(index*2+2, mid+1, r, left, right, plus)
	} else {
		segPlus(index*2+1, l, mid, left, mid, plus)
		segPlus(index*2+2, mid+1, r, mid+1, right, plus)
	}
	leftValue := seg[index*2+1] + lazy[index*2+1]
	rightValue := seg[index*2+2] + lazy[index*2+2]
	if leftValue < rightValue {
		seg[index] = leftValue
	} else {
		seg[index] = rightValue
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	writer := bytes.NewBuffer(make([]byte, 0, 1000000))
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	p, q, pos := make([]int, n), make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &p[i])
		p[i]--
		pos[p[i]] = i
		if i < n-1 {
			fmt.Fscanf(reader, " ")
		} else {
			fmt.Fscanf(reader, "\n")
		}
	}
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &q[i])
		q[i]--
		if i < n-1 {
			fmt.Fscanf(reader, " ")
		} else {
			fmt.Fscanf(reader, "\n")
		}
	}
	ans := n - 1
	for i := 0; i < n; i++ {
		for seg[0]+lazy[0] >= 0 {
			segPlus(0, 0, n-1, 0, pos[ans], -1)
			ans--
		}
		fmt.Fprintf(writer, "%d ", ans+2)
		segPlus(0, 0, n-1, 0, q[i], 1)
	}
	writer.WriteTo(os.Stdout)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func fillNNums(bs []byte, n int, res []int) {
	x := 0
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	n := readNum(reader)

	P := readNNums(reader, n)
	Q := readNNums(reader, n)

	res := solve(n, P, Q)

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", res[i])
	}
}

func solve(n int, P []int, Q []int) []int {
	arr := make([]int, 4*n)
	add := make([]int, 4*n)

	update := func(p int, x int) {
		add[p] += x
		arr[p] += x
	}

	push := func(p int) {
		if add[p] != 0 {
			update(p*2+1, add[p])
			update(p*2+2, add[p])
			add[p] = 0
		}
	}

	var updateRange func(i int, l, r int, tl, tr int, x int)

	updateRange = func(i int, l, r int, tl, tr int, x int) {
		if tl >= r || tr <= l {
			return
		}
		if tl >= l && tr <= r {
			update(i, x)
			return
		}
		push(i)

		mid := (tl + tr) / 2

		updateRange(2*i+1, l, r, tl, mid, x)
		updateRange(2*i+2, l, r, mid, tr, x)

		arr[i] = min(arr[2*i+1], arr[2*i+2])
	}

	rev := make([]int, n+1)
	for i := 0; i < n; i++ {
		rev[P[i]] = i
	}

	decrease := func(x int) bool {
		updateRange(0, 0, rev[x]+1, 0, n, -1)
		if arr[0] >= 0 {
			return true
		}
		updateRange(0, 0, rev[x]+1, 0, n, 1)
		return false
	}

	ans := make([]int, n)
	x := n

	for i := 0; i < n; i++ {
		for decrease(x) {
			x--
		}
		ans[i] = x
		updateRange(0, 0, Q[i], 0, n, 1)
	}

	return ans
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const N = 3000010
const INF = 1000000001
var n int
var a [N] int
var p [N] int
var pos[N] int

func max(a int, b int) int {
	if a < b {
		return b
	}
	return a
}

type SegNode struct {
	value int
	agg int
	max int
}

var Tree [4 * N] SegNode

func lazy(node int)  {
	if Tree[node].agg > 0 {
		Tree[node * 2].agg += Tree[node].agg
		Tree[node * 2 + 1].agg += Tree[node].agg
		Tree[node].agg = 0
	}
}

func addValue(node int, l int, r int, u int, v int, val int)  {
	if l > v || r < u {
		return
	}
	if l >= u && r <= v {
		Tree[node].agg += val
		return
	}

	var mid = (l + r) >> 1
	lazy(node)
	addValue(node * 2, l, mid, u, v, val)
	addValue(node * 2 + 1, mid + 1, r, u, v, val)
	Tree[node].max = max(Tree[node * 2].max + Tree[node * 2].agg, Tree[node * 2 + 1].max + Tree[node * 2 + 1].agg)
}

func getMax(node int, l int, r int, u int, v int) int {
	if r < u || l > v {
		return -INF
	}
	if l >= u && r <= v {
		return Tree[node].agg + Tree[node].max
	}
	var mid = (l + r) >> 1
	lazy(node)
	var maxValue = getMax(node * 2, l, mid, u, v)
	maxValue = max(maxValue, getMax(node * 2 + 1, mid + 1, r, u, v))
	return maxValue
}

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	fmt.Fscanf(reader, "%d\n", &n)
	for i:=0; i < n; i++ {
		fmt.Fscanf(reader,"%d ", &a[i])
		pos[a[i]] = i + 1
	}

	for i:=0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &p[i])
	}

	var curAns = n
	addValue(1, 1, n, 1, pos[curAns], 1)

	for i := 0; i < n; i++ {

		for true {
			var maxValue = getMax(1, 1, n, 1, n)
			if maxValue > 0 {
				break
			}
			curAns--
			addValue(1, 1, n, 1, pos[curAns],1)
		}

		fmt.Fprintf(writer, "%d ", curAns)
		addValue(1, 1, n, 1, p[i], -1)

	}
}