object D{
  def main(args: Array[String]){
//   import scala.collection.mutable.PriorityQueue
//    import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.BitSet
//    import scala.collection.mutable.Set
    import util.control.Breaks._
    import java.io.{PrintWriter}
    import java.util.{Locale, Scanner}

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
    val y=(for(i<- 0 until m) yield in.nextInt).toArray
    // index
/*    var A= collection.immutable.Set[Int]()
    for(k<- 1 to sub.length-1){
      A=A.filter((l)=>sub(k)==sub(k-l))

      if(sub(k)==sub(0)){
        A=A+k
      }
 }*/

    //    for(i<-0 until sub.length){out.print(A(i)+" ")}
    var sum=0

    var bad=false
    if(m!=0){
      var A=Array.fill(sub.length)(-1)
      var j= -1
      A(0)= -1
      for(i<-1 until sub.length){
        while(j>=0 && sub(i)!=sub(j+1)){
          j=A(j)
        }
        if(j== -1){
          if(sub(i)!=sub(0)){
            j= -2
          }
        }
        j+=1
        A(i)=j
      }
      sum=y(0)-1
      var B=Array.fill(sub.length)(true)
      var ll=sub.length-1
      ll=A(ll)
      while(ll>=0){
        B(sub.length-ll-1)=false
        ll=A(ll)
      }
      breakable{
        for(i<-1 until m){
          if(y(i)-y(i-1)>=sub.length){
            sum+=y(i)-y(i-1)-sub.length
          }
          else if(B(y(i)-y(i-1))){
            bad=true
            break
          }
        }
      }
    }
    if(m!=0 && n-y(m-1)+1<sub.length){
      bad=true
    }
    if(bad){
      out.println(0)
    }else{
      if(m==0){
        sum=n
      }else{
        sum+=n-y(m-1)-sub.length+1
      }
      var r=1L
      for(i<-0 until sum){
        r=(r*26L)%1000000007L
      }
      out.println(r)
    }

    out.flush
out.close
  }
}

object D{
  //import scala.collection.mutable.PriorityQueue
  //import scala.collection.mutable.ArrayBuffer
  import util.control.Breaks._
  import java.io.{PrintWriter,InputStream,BufferedReader,InputStreamReader}
  import java.util.{Locale, Scanner,StringTokenizer}
  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
    val reader=new BufferedReader(new InputStreamReader(stream), 32768);

    def next():String = {
        while (!st.hasMoreTokens()){
          val currentLine = reader.readLine
          st=new StringTokenizer(currentLine)
        }
        return st.nextToken
    }

    def nextInt():Int = {
        return next.toInt
    }

    def nextLong():Long = {
    	return next.toLong
    }
    def nextDouble:Double = {
      return next.toDouble
    }
  }

  def main(args: Array[String]){

    val in = new InputReader(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
//    nextLine
//    val y=(nextLine.split(' ').map((s)=>s.toInt)).toArray
    val y=Array.fill(m)(0)

    for(i<- 0 until m) {y(i)=nextInt}
    // index
/*    var A= collection.immutable.Set[Int]()
    for(k<- 1 to sub.length-1){
      A=A.filter((l)=>sub(k)==sub(k-l))

      if(sub(k)==sub(0)){
        A=A+k
      }
 }*/

    //    for(i<-0 until sub.length){out.print(A(i)+" ")}
    var sum=0

    var bad=false
    if(m!=0){
      val A=Array.fill(sub.length)(-1)
      var j= -1
      A(0)= -1
      for(i<-1 until sub.length){
        while(j>=0 && sub(i)!=sub(j+1)){
          j=A(j)
        }
        if(j== -1){
          if(sub(i)!=sub(0)){
            j= -2
          }
        }
        j+=1
        A(i)=j
      }
      sum=y(0)-1
      val B=Array.fill(sub.length)(true)
      var ll=sub.length-1
      ll=A(ll)
      while(ll>=0){
        B(sub.length-ll-1)=false
        ll=A(ll)
      }
      breakable{
        for(i<-1 until m){
          if(y(i)-y(i-1)>=sub.length){
            sum+=y(i)-y(i-1)-sub.length
          }
          else if(B(y(i)-y(i-1))){
            bad=true
            break
          }
        }
      }
    }
    if(m!=0 && n-y(m-1)+1<sub.length){
      bad=true
    }
    if(bad){
      out.println(0)
    }else{
      if(m==0){
        sum=n
      }else{
        sum+=n-y(m-1)-sub.length+1
      }
      var r=1L
      for(i<-0 until sum){
        r=(r*26L)%1000000007L
      }
      out.println(r)
    }

    out.flush
out.close
  }
}

import scala.collection._

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)
  val p = readLine
  val ys = if (m == 0) Array.empty else readInts(m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }

  val d = p.length - next(p.length)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0 && (y - prev < p.length)) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    val occupied = mutable.BitSet.empty
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        occupied += i
        i += 1
      }
      pos = r
    }
    val free = n - occupied.size
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

import scala.collection._

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)
  val p = readLine
  val ys = if (m == 0) Array.empty else readInts(m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }

  val d = p.length - next(p.length)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0 && (y - prev < p.length)) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    var free = n
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length
      while (i < r) {
        free -= 1
        i += 1
      }
      pos = r
    }
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, _m) = readInts(2)
  val p = readLine
  val ys = if (_m == 0) Array.empty else readInts(_m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }
  /*
  var j = 0
  val matchesBuilder = new ArrayBuilder.ofInt
  for (i <- p.indices) {
    while (j > 0 && p(j) != p(i)) j = next(j)
    if (p(j) == p(i)) j += 1
    println(i, j)
//    if (j == p.length - i) {
//      matchesBuilder += i - j + 1//p.length//matches += 1 // match at i - m
//      j = next(j)
//    }
    if (i == p.size - 1) println(j)
  }

  val matches = matchesBuilder.result
  val matchesSet = mutable.BitSet.empty ++ matches
  println(matches.mkString(" "))
  println(next.mkString(" "))
  if (!ys.forall(matchesSet)) println(0)
  */

  val d = p.length - next(p.length)
  //println(d)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0 && (y - prev < p.length)) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    val occupied = mutable.BitSet.empty
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        occupied += i
        i += 1
      }
      pos = r
    }
    val free = n - occupied.size
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

object D{
  //import scala.collection.mutable.PriorityQueue
  //import scala.collection.mutable.ArrayBuffer
  import util.control.Breaks._
  import java.io.{PrintWriter,InputStream,BufferedReader,InputStreamReader}
  import java.util.{Locale, Scanner,StringTokenizer}
  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
    val reader=new BufferedReader(new InputStreamReader(stream), 32768);

    def next():String = {
        while (!st.hasMoreTokens()){
          val currentLine = reader.readLine
          st=new StringTokenizer(currentLine)
        }
        return st.nextToken
    }

    def nextInt():Int = {
        return next.toInt
    }

    def nextLong():Long = {
    	return next.toLong
    }
    def nextDouble:Double = {
      return next.toDouble
    }
  }

  def main(args: Array[String]){

    val in = new InputReader(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
//    nextLine
//    val y=(nextLine.split(' ').map((s)=>s.toInt)).toArray
   // val y=Array.fill(m)(0)
    val y= (for(_<- 0 until m) yield nextInt).toArray
//    for(i<- 0 until m) {y(i)=nextInt}
    // index
/*    var A= collection.immutable.Set[Int]()
    for(k<- 1 to sub.length-1){
      A=A.filter((l)=>sub(k)==sub(k-l))

      if(sub(k)==sub(0)){
        A=A+k
      }
 }*/

    //    for(i<-0 until sub.length){out.print(A(i)+" ")}
    var sum=0

    var bad=false
    if(m!=0){
      val A=Array.fill(sub.length)(-1)
      var j= -1
      A(0)= -1
      for(i<-1 until sub.length){
        while(j>=0 && sub(i)!=sub(j+1)){
          j=A(j)
        }
        if(j== -1){
          if(sub(i)!=sub(0)){
            j= -2
          }
        }
        j+=1
        A(i)=j
      }
      sum=y(0)-1
      val B=Array.fill(sub.length)(true)
      var ll=sub.length-1
      ll=A(ll)
      while(ll>=0){
        B(sub.length-ll-1)=false
        ll=A(ll)
      }
      breakable{
        for(i<-1 until m){
          if(y(i)-y(i-1)>=sub.length){
            sum+=y(i)-y(i-1)-sub.length
          }
          else if(B(y(i)-y(i-1))){
            bad=true
            break
          }
        }
      }
    }
    if(m!=0 && n-y(m-1)+1<sub.length){
      bad=true
    }
    if(bad){
      out.println(0)
    }else{
      if(m==0){
        sum=n
      }else{
        sum+=n-y(m-1)-sub.length+1
      }
      var r=1L
      for(i<-0 until sum){
        r=(r*26L)%1000000007L
      }
      out.println(r)
    }

    out.flush
out.close
  }
}

object D{
  def main(args: Array[String]){
//   import scala.collection.mutable.PriorityQueue
//    import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.BitSet
//    import scala.collection.mutable.Set
    import util.control.Breaks._
    import java.io.{PrintWriter}
    import java.util.{Locale, Scanner}

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    def nextLine=in.nextLine
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
//    nextLine
//    val y=(nextLine.split(' ').map((s)=>s.toInt)).toArray
    val y=(for(_<- 0 until m)yield nextInt).toArray
    // index
/*    var A= collection.immutable.Set[Int]()
    for(k<- 1 to sub.length-1){
      A=A.filter((l)=>sub(k)==sub(k-l))

      if(sub(k)==sub(0)){
        A=A+k
      }
 }*/

    //    for(i<-0 until sub.length){out.print(A(i)+" ")}
    var sum=0

    var bad=false
    if(m!=0){
      val A=Array.fill(sub.length)(-1)
      var j= -1
      A(0)= -1
      for(i<-1 until sub.length){
        while(j>=0 && sub(i)!=sub(j+1)){
          j=A(j)
        }
        if(j== -1){
          if(sub(i)!=sub(0)){
            j= -2
          }
        }
        j+=1
        A(i)=j
      }
      sum=y(0)-1
      val B=Array.fill(sub.length)(true)
      var ll=sub.length-1
      ll=A(ll)
      while(ll>=0){
        B(sub.length-ll-1)=false
        ll=A(ll)
      }
      breakable{
        for(i<-1 until m){
          if(y(i)-y(i-1)>=sub.length){
            sum+=y(i)-y(i-1)-sub.length
          }
          else if(B(y(i)-y(i-1))){
            bad=true
            break
          }
        }
      }
    }
    if(m!=0 && n-y(m-1)+1<sub.length){
      bad=true
    }
    if(bad){
      out.println(0)
    }else{
      if(m==0){
        sum=n
      }else{
        sum+=n-y(m-1)-sub.length+1
      }
      var r=1L
      for(i<-0 until sum){
        r=(r*26L)%1000000007L
      }
      out.println(r)
    }

    out.flush
out.close
  }
}

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)
  val p = readLine
  val ys = if (m == 0) Array.empty else readInts(m).map(_ - 1)

  val next = Array.fill(p.length + 1)(0)
  var k = 0
  for (j <- 1 until p.length) {
    while (k > 0 && p(k) != p(j)) k = next(k)
    if (p(k) == p(j)) k += 1
    next(j + 1) = k
  }

  val d = p.length - next(p.length)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0 && y - prev < p.length) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    var free = n
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length
      while (i < r) {
        free -= 1
        i += 1
      }
      pos = r
    }
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

object D{
  //import scala.collection.mutable.PriorityQueue
  //import scala.collection.mutable.ArrayBuffer
  import util.control.Breaks._
  import java.io.{PrintWriter}
  import java.util.{Locale, Scanner}

  def main(args: Array[String]){

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    def nextLine=in.nextLine
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
//    nextLine
//    val y=(nextLine.split(' ').map((s)=>s.toInt)).toArray
    val y=Array.fill(m)(0)

    for(i<- 0 until m) {y(i)=nextInt}
    // index
/*    var A= collection.immutable.Set[Int]()
    for(k<- 1 to sub.length-1){
      A=A.filter((l)=>sub(k)==sub(k-l))

      if(sub(k)==sub(0)){
        A=A+k
      }
 }*/

    //    for(i<-0 until sub.length){out.print(A(i)+" ")}
    var sum=0

    var bad=false
    if(m!=0){
      val A=Array.fill(sub.length)(-1)
      var j= -1
      A(0)= -1
      for(i<-1 until sub.length){
        while(j>=0 && sub(i)!=sub(j+1)){
          j=A(j)
        }
        if(j== -1){
          if(sub(i)!=sub(0)){
            j= -2
          }
        }
        j+=1
        A(i)=j
      }
      sum=y(0)-1
      val B=Array.fill(sub.length)(true)
      var ll=sub.length-1
      ll=A(ll)
      while(ll>=0){
        B(sub.length-ll-1)=false
        ll=A(ll)
      }
      breakable{
        for(i<-1 until m){
          if(y(i)-y(i-1)>=sub.length){
            sum+=y(i)-y(i-1)-sub.length
          }
          else if(B(y(i)-y(i-1))){
            bad=true
            break
          }
        }
      }
    }
    if(m!=0 && n-y(m-1)+1<sub.length){
      bad=true
    }
    if(bad){
      out.println(0)
    }else{
      if(m==0){
        sum=n
      }else{
        sum+=n-y(m-1)-sub.length+1
      }
      var r=1L
      for(i<-0 until sum){
        r=(r*26L)%1000000007L
      }
      out.println(r)
    }

    out.flush
out.close
  }
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, _m) = readInts(2)
  val p = readLine
  val ys = readInts(_m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }
/*
  var j = 0
  val matchesBuilder = new ArrayBuilder.ofInt
  for (i <- p.indices) {
    while (j > 0 && p(j) != p(i)) j = next(j)
    if (p(j) == p(i)) j += 1
    println(i, j)
//    if (j == p.length - i) {
//      matchesBuilder += i - j + 1//p.length//matches += 1 // match at i - m
//      j = next(j)
//    }
    if (i == p.size - 1) println(j)
  }

  val matches = matchesBuilder.result
  val matchesSet = mutable.BitSet.empty ++ matches
  println(matches.mkString(" "))
  println(next.mkString(" "))
  if (!ys.forall(matchesSet)) println(0)
  */

  val d = p.length - next(p.length)
  //println(d)

  var prev = ys.head
  var ok = true
  for (y <- ys.tail) {
    if ((y - prev) % d != 0) ok = false
    prev = y
  }

  if (!ok) println(0)
  else {
    val occupied = mutable.BitSet.empty
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        occupied += i
        i += 1
      }
      pos = r
    }
    val free = n - occupied.size
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)
  val p = readLine
  val ys = if (m == 0) Array.empty else readInts(m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }

  val d = p.length - next(p.length)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0 && (y - prev < p.length)) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    var free = n
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        free -= 1
        i += 1
      }
      pos = r
    }
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, _m) = readInts(2)
  val p = readLine
  val ys = readInts(_m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length + 1) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }
  /*
  var j = 0
  val matchesBuilder = new ArrayBuilder.ofInt
  for (i <- p.indices) {
    while (j > 0 && p(j) != p(i)) j = next(j)
    if (p(j) == p(i)) j += 1
    println(i, j)
//    if (j == p.length - i) {
//      matchesBuilder += i - j + 1//p.length//matches += 1 // match at i - m
//      j = next(j)
//    }
    if (i == p.size - 1) println(j)
  }

  val matches = matchesBuilder.result
  val matchesSet = mutable.BitSet.empty ++ matches
  println(matches.mkString(" "))
  println(next.mkString(" "))
  if (!ys.forall(matchesSet)) println(0)
  */

  val d = p.length - next(p.length)
  //println(d)

  var ok = true
  if (ys.nonEmpty) {
    var prev = ys.head
    for (y <- ys.tail) {
      if ((y - prev) % d != 0) ok = false
      prev = y
    }
  }

  if (!ok) println(0)
  else {
    val occupied = mutable.BitSet.empty
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        occupied += i
        i += 1
      }
      pos = r
    }
    val free = n - occupied.size
    val res = if (free < 0) 0 else BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, _m) = readInts(2)
  val p = readLine
  val ys = readInts(_m).map(_ - 1)

  val next = Array.fill(2 * p.length + 2)(0)
  var k = 0
  for (j <- 1 until 2 * p.length) {
    while (k > 0 && (j < p.length && p(k) != p(j))) k = next(k)
    if (j >= p.length || p(k) == p(j)) k += 1
    next(j + 1) = k
  }

  var j = 0
  val matchesBuilder = new ArrayBuilder.ofInt
  for (i <- p.indices) {
    while (j > 0 && p(j) != p(i)) j = next(j)
    if (p(j) == p(i)) j += 1
    if (j == p.length - i) {
      matchesBuilder += i - j + 1//p.length//matches += 1 // match at i - m
      j = next(j)
    }
  }

  val matches = matchesBuilder.result
  val matchesSet = mutable.BitSet.empty ++ matches

  if (!ys.forall(matchesSet)) println(0)
  else {
    val occupied = mutable.BitSet.empty
    var pos = 0
    for (y <- ys) {
      var i = y max pos
      val r = y + p.length - 1
      while (i <= r) {
        occupied += i
        i += 1
      }
      pos = r
    }
    val free = n - occupied.size
    val res = BigInt(26).modPow(free, 1000000007L)
    println(res)
  }
}

object D{
  def main(args: Array[String]){
//   import scala.collection.mutable.PriorityQueue
//    import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.TreeSet
    import util.control.Breaks._
    import java.io.{PrintWriter}
    import java.util.{Locale, Scanner}

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    val n=in.nextInt
    val m=in.nextInt
    val sub=in.next
    val y=(for(i<- 0 until m) yield in.nextInt).toArray
    // index
    val A=new TreeSet[Int]()
    for(k<- 1 to sub.length-1){
      for(l <- A){
        if(sub(k)!=sub(k-l)){
          A-=l
        }
      }
      if(sub(k)==sub(0)){
        A+=k
      }
    }
    var sum=y(0)-1
    var bad=false
    for(i<-1 until m){
      if(y(i)-y(i-1)>=sub.length){
        sum+=y(i)-y(i-1)-sub.length
      }
      else if(!A(y(i)-y(i-1))){
        bad=true
      }
    }
    if(bad){
      out.println(0)
    }
    else{
      if(n-y(m-1)+1>=sub.length){
        sum+=n-y(m-1)-sub.length+1
        var r=1
        for(i<-0 until sum){
          r=(r*26)%1000000007
        }
        out.println(r)
      }
      else{
        out.println(0)
      }
    }
    out.flush
out.close
  }
}


import scala.collection.SortedSet
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.io.StdIn

object Main extends App {
  def intPair(s: String): (Int, Int) = {
    val (s1, s2) = s.span(_ != ' ')
    (s1.toInt, s2.trim().toInt)
  }

  val m :: k :: List() = StdIn.readLine().split(" ").map(_.toInt).toList

  val input = (1 to m).map { _ => StdIn.readLine() }
  val pairs = TreeSet(input.map(intPair).flatMap(p => List(p, p.swap)) :_*)

  val friends = TreeMap(pairs.groupBy { case (a, b) => a}.mapValues(_.map { case (a, b) => b}).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me &&
      !myFriends.contains(person) &&
      (friends(person) & myFriends).size * 100 >= k * myFriends.size
  }

  def suggestedFriends(me: Int) : List[Int] = people.filter(isSuggested(_, me))

  val result = TreeMap((people zip people.map(suggestedFriends)).toSeq :_*)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.collection.mutable.ListBuffer
import scala.io.StdIn

object Main extends App {
  val m :: k :: List() = StdIn.readLine().split(" ").map(_.toInt).toList
  val lines = (1 to m).map(x => StdIn.readLine())

  val pairs = TreeSet(lines.flatMap(_.split(" ").map(_.toInt).toList match {
    case a :: b :: List() => List((a, b), (b, a))
  }).toList: _*)

  val friends = TreeMap(pairs.groupBy { case (a, b) => a}.mapValues(_.map { case (a, b) => b}).toSeq: _*)

  for (me <- friends.keySet) {
    print(s"$me: ")
    val result = ListBuffer[Int]()
    val myFriends = friends(me)

    for (person <- friends.keySet) {
      var s = 0
      if (person != me && !pairs.contains(person -> me)) {
        for (friend <- myFriends) {
          if (pairs.contains(friend -> person)) {
            s += 1
          }
        }

        if (s * 100 >= k * myFriends.size) {
          result += person
        }
      }
    }

    print(s"${result.size} ")
    println(result.mkString(" "))
  }
}

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet

object Main extends App with IOUtils {
  val (m, k) = nextIntPair
  val pairsList = (1 to m).map { _ => nextIntPair }

  val pairs = TreeSet(pairsList.flatMap(p => List(p, p.swap)) :_*)

  val friends = TreeMap(pairs.groupBy { case (a, b) => a}.mapValues(_.map { case (a, b) => b}).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me &&
      !myFriends.contains(person) &&
      (friends(person) & myFriends).size * 100 >= k * myFriends.size
  }

  def suggestedFriends(me: Int) : List[Int] = people.filter(isSuggested(_, me))

  val result = TreeMap((people zip people.map(suggestedFriends)).toSeq :_*)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

trait IOUtils {
  var in: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
  var tok: StringTokenizer = null

  def nextInt: Int = nextToken.toInt
  def nextLong: Long = nextToken.toLong
  def nextIntPair: (Int, Int) = (nextInt, nextInt)

  def nextToken: String = {
    if (tok == null || !tok.hasMoreTokens) {
      tok = new StringTokenizer(in.readLine)
    }

    tok.nextToken
  }
}

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

import scala.collection.immutable.HashSet
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet

object Main extends App with IOUtils {
  val (m, k) = nextIntPair

  def pairsList = (1 to m).map { _ => nextIntPair}

  val pairs = TreeSet(pairsList.flatMap(p => List(p, p.swap)): _*)

  val friends = TreeMap(pairs.groupBy(_._1).mapValues(_.map(_._2)).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me &&
      !myFriends.contains(person) &&
      (friends(person) & myFriends).size * 100 >= k * myFriends.size
  }

  def suggestedFriends(me: Int): List[Int] = people.filter(isSuggested(_, me))

  val result = people zip (people map suggestedFriends)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

trait IOUtils {
  var in: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
  var tok: StringTokenizer = null

  def nextInt: Int = nextToken.toInt

  def nextLong: Long = nextToken.toLong

  def nextIntPair: (Int, Int) = (nextInt, nextInt)

  def nextToken: String = {
    if (tok == null || !tok.hasMoreTokens) {
      tok = new StringTokenizer(in.readLine)
    }

    tok.nextToken
  }
}

import scala.collection.SortedSet
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.io.StdIn

object Main extends App {
  val m :: k :: List() = StdIn.readLine().split(" ").map(_.toInt).toList

  val pairs = TreeSet((1 to m).flatMap(x => StdIn.readLine().split(" ").map(_.toInt).toList match {
    case a :: b :: List() => List((a, b), (b, a))
  }).toList: _*)

  val friends = TreeMap(pairs.groupBy { case (a, b) => a}.mapValues(_.map { case (a, b) => b}).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me &&
      !myFriends.contains(person) &&
      (friends(person) & myFriends).size * 100 >= k * myFriends.size
  }

  def suggestedFriends(me: Int) : List[Int] = people.filter(isSuggested(_, me))

  val result = TreeMap((people zip people.map(suggestedFriends)).toSeq :_*)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

import java.io.InputStreamReader
import java.io.BufferedReader

import java.util.StringTokenizer

import scala.collection.immutable.TreeMap
import scala.collection.mutable.HashSet
import scala.collection.mutable.ListBuffer

object Main extends App {
  var in: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
  var tok: StringTokenizer = null

  def nextInt: Int = nextToken.toInt

  def nextLong: Long = nextToken.toLong

  def nextToken: String = {
    if (tok == null || !tok.hasMoreTokens) {
      tok = new StringTokenizer(in.readLine)
    }

    tok.nextToken
  }

  val m: Int = nextInt
  val k: Int = nextInt
  var friends = TreeMap[Int, List[Int]]()
  val pairs = new HashSet[(Int, Int)]

  for (i <- 1 to m) {
    val a: Int = nextInt
    val b: Int = nextInt
    if (!friends.contains(a)) {
      friends += a -> List[Int]()
    }

    friends += a -> (b :: friends(a))

    if (!friends.contains(b)) {
      friends += b -> List[Int]()
    }

    friends += b -> (a :: friends(b))

    pairs += a -> b
    pairs += b -> a
  }

  for (me <- friends.keySet) {
    print(s"$me: ")
    val result = ListBuffer[Int]()
    val myFriends = friends(me)

    for (person <- friends.keySet) {
      var s = 0
      if (person != me && !pairs.contains(person -> me)) {
        for (friend <- myFriends) {
          if (pairs.contains(friend -> person)) {
            s += 1
          }
        }

        if (s * 100 >= k * myFriends.size) {
          result += person
        }
      }
    }

    print(s"${result.size} ")
    println(result.mkString(" "))
  }
}

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

import scala.collection.immutable.HashSet
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet

object Main extends App with IOUtils {
  val (m, k) = nextIntPair

  def pairsList = (1 to m).map { _ => nextIntPair}

  val pairs = TreeSet(pairsList.flatMap(p => List(p, p.swap)): _*)

  val friends = pairs.foldLeft(TreeMap[Int, Set[Int]]()) { (m, p) =>
    m + (p._1 -> (m.getOrElse(p._1, Set()) + p._2))
  }

//  val friends = TreeMap(pairs.groupBy(_._1).mapValues(_.map(_._2)).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me &&
      !myFriends.contains(person) &&
      (friends(person) & myFriends).size * 100 >= k * myFriends.size
  }

  def suggestedFriends(me: Int): List[Int] = people.filter(isSuggested(_, me))

  val result = TreeMap((people zip people.map(suggestedFriends)).toSeq: _*)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

trait IOUtils {
  var in: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
  var tok: StringTokenizer = null

  def nextInt: Int = nextToken.toInt

  def nextLong: Long = nextToken.toLong

  def nextIntPair: (Int, Int) = (nextInt, nextInt)

  def nextToken: String = {
    if (tok == null || !tok.hasMoreTokens) {
      tok = new StringTokenizer(in.readLine)
    }

    tok.nextToken
  }
}

import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.io.StdIn

object Main extends App {
  val m :: k :: List() = StdIn.readLine().split(" ").map(_.toInt).toList

  val pairs = TreeSet(Source.stdin.getLines().flatMap(_.split(" ").map(_.toInt).toList match {
    case a :: b :: List() => List((a, b), (b, a))
  }).toList :_*)

  val friends = TreeMap(pairs.groupBy{ case (a, b) => a }.mapValues(_.map { case (a, b) => b }).toSeq :_*)

  for (me <- friends.keySet) {
    print(s"$me: ")
    val result = ListBuffer[Int]()
    val myFriends = friends(me)

    for (person <- friends.keySet) {
      var s = 0
      if (person != me && !pairs.contains(person -> me)) {
        for (friend <- myFriends) {
          if (pairs.contains(friend -> person)) {
            s += 1
          }
        }

        if (s * 100 >= k * myFriends.size) {
          result += person
        }
      }
    }

    print(s"${result.size} ")
    println(result.mkString(" "))
  }
}

import scala.collection.SortedSet
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.io.StdIn

object Main extends App {
  val m :: k :: List() = StdIn.readLine().split(" ").map(_.toInt).toList

  val pairs = TreeSet((1 to m).flatMap(x => StdIn.readLine().split(" ").map(_.toInt).toList match {
    case a :: b :: List() => List((a, b), (b, a))
  }).toList: _*)

  val friends = TreeMap(pairs.groupBy { case (a, b) => a}.mapValues(_.map { case (a, b) => b}).toSeq: _*)
  val people = friends.keySet.toList

  def isSuggested(person: Int, me: Int): Boolean = {
    val myFriends = friends(me)
    person != me && ((friends(person) & myFriends).size * 100 >= k * myFriends.size)
  }

  def suggestedFriends(me: Int) : List[Int] = people.filter(isSuggested(_, me))

  val result = TreeMap((people zip people.map(suggestedFriends)).toSeq :_*)

  result.foreach { case (me, suggested) =>
    println(s"$me: ${suggested.size} ${suggested.mkString(" ")}")
  }
}

import java.io.InputStreamReader
import java.io.BufferedReader

import java.util.StringTokenizer

import scala.collection.immutable.TreeMap
import scala.collection.mutable.HashSet
import scala.collection.mutable.ListBuffer

object Main extends App {
  var in: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
  var tok: StringTokenizer = null

  def nextInt: Int = nextToken.toInt

  def nextLong: Long = nextToken.toLong

  def nextToken: String = {
    if (tok == null || !tok.hasMoreTokens) {
      tok = new StringTokenizer(in.readLine)
    }

    tok.nextToken
  }

  val m: Int = nextInt
  val k: Int = nextInt
  var friends = TreeMap[Int, List[Int]]()
  val pairs = new HashSet[(Int, Int)]

  for (i <- 1 to m) {
    val a: Int = nextInt
    val b: Int = nextInt
    if (!friends.contains(a)) {
      friends += a -> List[Int]()
    }

    friends += a -> (b :: friends(a))

    if (!friends.contains(b)) {
      friends += b -> List[Int]()
    }

    friends += b -> (a :: friends(b))

    pairs += a -> b
    pairs += b -> a
  }

  for (me <- friends.keySet) {
    print(s"$me: ")
    val result = ListBuffer[Int]()
    val myFriends = friends(me)

    for (person <- friends.keySet) {
      var s = 0
      if (person != me && !pairs.contains(person -> me)) {
        for (friend <- myFriends) {
          if (pairs.contains(friend -> person)) {
            s += 1
          }
        }
      }

      if (s * 100 >= k * myFriends.size) {
        result += person
      }
    }

    print(s"${result.size} ")
    println(result.mkString(" "))
  }
}


object A136 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val input = readInts(n).zipWithIndex.toMap.mapValues(_+1)
    val res = (1 to n).foldLeft(List.empty[Int]) {case (arr, i) => arr ++ List(input(i))}
    println(res.mkString(" "))
  }
}

object Main {

  def main(args: Array[String]) {

    val std = scala.io.StdIn
    val n = std.readLine().toInt
    val values = std.readLine().split(" ").map(_.toInt)
    val result = new Array[Int](values.length)
    for (i <- 0 until n) {
      result(values(i)-1) = i + 1
    }
    println(result.mkString(" "))
  }
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P136A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val presenters = new Array[Int](N)

  for (i <- 1 to N)
    presenters(sc.nextInt - 1) = i

  out.println(presenters.mkString(" "))
  out.close
}

object Presents {
	def main (args: Array[String]) {
		val scanner = new java.util.Scanner(System.in);
		val n = scanner.nextInt;
		var function = new Array[Int](n);
		for (i <- 1 to n) {
			val value = scanner.nextInt;
					function(value-1) = i
		}
		for (i <- 1 to n) {
		  print(function(i-1) + " ")
		}
	}
}

import java.io._
import scala.annotation.tailrec

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInt = reader.readLine().toInt
  def readInts = reader.readLine().split(" ").map(_.toInt)

  val n = readInt
  def ans = readInts.zip(1 to n).sortBy(_._1).map(_._2).mkString(" ")

  def main(a: Array[String]) {
    println(ans)
  }
}

object Main {
  def main(args: Array[String]) {
    val i = readInt()
    val a = readLine.split(" ").map(_.toInt)
    val r = new Array[Int](a.size)
    for(i <- 0 until a.size) { r(a(i) - 1) = i + 1 }
    println(r.mkString(" "))
  }
}

// Codeforces 136A

object _136A extends App {
  val scanner = new java.util.Scanner(System.in)
  val n = scanner.nextInt
  val presents = Array.fill(n)(scanner.nextInt)
  val answer = Array.tabulate(n)(i => presents.indexOf(i+1)+1)
  println(answer.mkString(" "))
}

import Array._
import scala.io.StdIn.readLine
object problem {
  def main(args: Array[String]) {
  val n = readLine.toInt
    val s = new Array[Int](n + 1)
    val A = readLine.split(" ").map(_.toInt)
    for(i <- 0 until n){
      s(A(i)) = i + 1
    }
    for(i <- 1 to n) print(s(i) + " ")
  }
}

import java.util.Scanner

object HelloWorldMain {

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val n = sc.nextInt()

    var array = new Array[Int](n)

    for( i  <- 1 to n ){
      array(sc.nextInt()-1) = i

    }

    array.foreach(c => print(c  + " "))
  }
}

import scala.io.StdIn.readInt
import scala.io.StdIn.readLine

object Presents {
  def main(args: Array[String]) {
     val n = readInt
     println((0 +: readLine.split(' ').map(_.toInt)).
         zipWithIndex.sortBy(_._1).tail.map(_._2).mkString(" "))
  }

}

import scala.util.Sorting
import scala.io.StdIn

object App {

  def main(args: Array[String]) {

    val n = StdIn.readLine().toInt

    val B = StdIn.readLine().split(" ").map(_.toInt)
    val A:Array[Int] = new Array[Int](n)
    for(i <- 0 to n - 1) {
      A(B(i) - 1) = i + 1;
    }

    println(A.mkString(" "))

  }

}

import java.util.Scanner

object TEST extends App {
    val n =readInt()
    val a : List[Int] = readLine.split(' ').toList.map(_.toInt)
    val ans = (for(i <- 1 to n) yield a.indexOf(i)+1).mkString(" ")
    print(ans)
}

import scala.io.StdIn._

object Task extends App{
  val n = readInt
  val ps = readLine.split(" ").map(_.toInt)

  val ans = ps.zipWithIndex.sortBy(_._1).map(_._2 + 1)

  println(ans.mkString(" "))
}

import java.util.Scanner

object MainA extends App {
  val sc = new Scanner(System.in)

  val N = sc.nextInt
  val l = for(t <- 0 until N) yield sc.nextInt
  val res = for(i <- 1 to N) yield l.indexOf(i) + 1
  println(res.mkString(" "))
}

object One36A extends App {
	import java.io.PrintWriter
	import java.util.Scanner

	val in = new Scanner(System.in)
	val out = new PrintWriter(System.out)

	import in._
	import out._

	val n = nextInt
	val arr = new Array[Int](n)

	(0 until n).foreach { i =>
		val no = nextInt
		arr(no-1) = i+1
	}
	println(arr.mkString(" "))

	in.close
	out.close
}

object Main extends App {
  def iterate(pointer: Int): Unit = {
    if (pointer == numberOfFriends) return
    else giftedByIdArray(giftMapArray(pointer) - 1) = pointer + 1; iterate(pointer + 1)
  }

  val numberOfFriends = readLine.toInt
  val giftMapArray = readLine.split(" ").map(_.toInt)
  val giftedByIdArray = new Array[Int](numberOfFriends)
  iterate(0)

  println(giftedByIdArray.mkString(" "))
}

object Solution136A extends App {

  def solution() {
    val n = _int
    1.to(n).map(_ => _int).zip(1.to(n)).sortBy(_._1).foreach(p => print(p._2 + " "))
  }

  //////////////////////////////////////////////////
  import java.util.Scanner
  val SC: Scanner = new Scanner(System.in)
  def _line: String = SC.nextLine
  def _int: Int = SC.nextInt
  def _long: Long = SC.nextLong
  def _string: String = SC.next
  solution()
}

import scala.collection.mutable._
import scala.math._
import scala.util._
import scala.util.control.Breaks.break
import scala.util.control.Breaks.breakable
import java.util._
object Main extends App {
  val sc = new Scanner(System.in)
  val n = sc.nextInt
  val t = for(i <- 1 to n) yield sc.nextInt
  val s = for(i <- 1 to n) yield t.indexOf(i) + 1
  println(s.mkString(" "))
}

object Solution extends App{
  val in = scala.io.Source.stdin.getLines()
  in.next()
  val data = in.next().split(" ").map(_.toInt)
  var answer = Array.ofDim[Int](data.length)
  data.zipWithIndex.foreach {
    case(index, value) => answer(index - 1) = value + 1
  }
  println(answer.mkString(" "))
}


object TwoDistinctPoints extends App {

  val q = io.StdIn.readLine().toInt

  for (_ <- 1 to q) {

    val line = io.StdIn.readLine.split(" ").map(_.toInt)
    val (l1, r1, l2, r2) = (line(0), line(1), line(2), line(3))

    val firstAns  = l1
    val secondAns = if (l2 == firstAns) l2 + 1 else l2

    println(s"$firstAns $secondAns")
  }
}

//package codeforces.contest1108

object TwoDistinctPoints {
  def main(args: Array[String]): Unit = {
    val q = io.StdIn.readInt()
    (1 to q).foreach { _ =>
      val Array(l1, r1, l2, r2) = io.StdIn.readLine.split(" ").map(_.toInt)

      val result: List[(Int, Int)] = for {
        a <- List(l1, r1)
        b <- List(l2, r2)
        if a != b
      } yield (a, b)

      println(result.head._1 + " " + result.head._2)
    }
  }

}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    REP(ni()) { _ =>
      val l1, r1, l2, r2 = ni()
      if (l1 == r2) {
        out.println(s"$r1 $l1")
      } else {
        out.println(s"$l1 $r2")
      }
    }
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.io.StdIn

object A1108 {
  def main(args: Array[String]): Unit = {
    val n = StdIn.readInt()
    for {i <- 1 to n} {
      val Array(l1, r1, l2, r2) = StdIn.readLine().split(' ').map(_.toInt)
      println(l1 + " " + (if (l1 == l2) r2 else l2))
    }
  }
}

object Main {

  def main(args: Array[String]) {

    val std = scala.io.StdIn
    val n = std.readLine().toInt

    for (_ <- 0 until n) {
      val values = std.readLine().split(" ").map(_.toInt)
      if (values(0) <= values(2)) {
        println(values(0) + " " + values(3))
      } else {
        println(values(1) + " " + values(2))
      }
    }
  }
}

object CF_535_3_A {
// date: 25/01/2019

  type In  = Seq[Seq[Int]]
  type Out = Seq[(Int, Int)]

  def solve(in: In): Out = {
    val (xs) = in
    for {
      Seq(l1,r1,l2,r2) <- xs
    } yield {
      val b = if(l2 != l1) l2
                     else l2 + 1
      (l1, b)
    }
  }

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Specify Input and Output formats on RHS here:

  def formatIn(i: Input): In       = i.getLines(i.int).map(_.intSeq)
  def formatOut(out: Out): String  = out.map{case (a,b) => s"$a $b"}.mkString("\n")

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Boilerplate & utility methods that don't change (so ignore):

  import java.util.Scanner
  import java.io.InputStream
  import language.implicitConversions

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  class Input(val sc: Scanner) {
    // This class is useful for convenience, and for mock stdin in test scripts
    // Remember to call nextLine to advance past the newline character (if required) before taking a whole line
    def this(i: InputStream)       = this(new Scanner(i))
    def this(s: String)            = this(new Scanner(s.stripMargin))
    def int                        = sc.nextInt()
    def long                       = sc.nextLong()
    def double                     = sc.nextDouble()
    def nextLine                   = sc.nextLine()
    def collect[T](f: String => T) = sc.nextLine().split(" ").toVector.map(f)
    def intSeq                     = collect(_.toInt)
    def doubleSeq                  = collect(_.toDouble)
    def getLines(): Vector[String]   = if (!sc.hasNextLine) Vector.empty
                                     else sc.nextLine +: getLines
    def getLines(lineCount: Int)   = {nextLine; (1 to lineCount).map(_ => sc.nextLine)}
    def solveVal: Out              = (solve _).tupled(formatIn(this))
    def solveStr: String           = formatOut(solveVal)
  }

  // Ceiling division for Int & Long
  // `a` MUST be > 0. Incorrect otherwise.
  def divideCeil(a: Int, b: Int)   = (a - 1)/b + 1
  def divideCeil(a: Long, b: Long) = (a - 1)/b + 1

  // A frequently used number in these comps
  val modulo = 1000000007

  // For convenience in test scripts, treat a String as an Input
  implicit def stringToInput(s: String): Input = new Input(s)
  // Required to make `solveVal` work when input has just 1 value (i.e. not a Tuple)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) {
    def tupled: T => R = x => f(x)
  }
}


import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, q) = in.next().split(' ').map(_.toInt)
  val data = in.next().split(' ').map(_.toInt).sorted
  val frequency = Array.ofDim[Int](n)
  (1 to q).foreach { _ =>
    val Array(l, r) = in.next().split(' ').map(_.toInt - 1)
    frequency(l) += 1
    if (r != n - 1)
      frequency(r + 1) -= 1
  }
  val sortedFreq = frequency.foldLeft((List.empty[Long], 0l)) {
    case((list, soFar), i) => (soFar + i :: list, soFar + i)
  }._1.sorted
  println(sortedFreq.zip(data).foldLeft(0l) {
    case(acc, (count, value)) => acc + count * value
  })

}

object Main {
  private def scalaMergeSort(a: Array[Long], lo: Int, hi: Int, scratch: Array[Long]) {
    if (lo < hi) {
      val mid = (lo+hi) / 2
      scalaMergeSort(a, lo, mid, scratch)
      scalaMergeSort(a, mid+1, hi, scratch)
      var k, t_lo = lo
      var t_hi = mid + 1
      while (k <= hi) {
        if ((t_lo <= mid) && ((t_hi > hi) || (a(t_lo) >= a(t_hi)))) {
          scratch(k) = a(t_lo)
          t_lo += 1
        } else {
          scratch(k) = a(t_hi)
          t_hi += 1
        }
        k += 1
      }
      k = lo
      while (k <= hi) {
        a(k) = scratch(k)
        k += 1
      }
    }
  }

  def scalaMergeSortPerform(a: Array[Long]) {
    scalaMergeSort(a, 0, a.length - 1, Array.ofDim[Long](a.length))
  }

  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || (al_next <= m && a(al_next) < a(am_next))) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    //Array.copy(b, l, a, l, r - l + 1)
    for (i <- l to r)
      a(i) = b(i)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    scalaMergeSortPerform(array)
    scalaMergeSortPerform(b)
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
//import java.util.Arrays
object Main {
  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || (al_next <= m && a(al_next) < a(am_next))) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    //Array.copy(b, l, a, l, r - l + 1)
    for (i <- l to r)
      a(i) = b(i)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    /*Arrays.sort(array)
    Arrays.sort(b)*/
    /*Sorting.stableSort(array)
    Sorting.stableSort(b)*/
    mergeSort(array)
    mergeSort(b)
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

//import scala.util.Sorting
//import java.util.Arrays
object Main {

  private def scalaMergeSort(a: Array[Long], lo: Int, hi: Int, scratch: Array[Long], f: (Long,Long) => Boolean) {
    if (lo < hi) {
      val mid = (lo+hi) / 2
      scalaMergeSort(a, lo, mid, scratch, f)
      scalaMergeSort(a, mid+1, hi, scratch, f)
      var k, t_lo = lo
      var t_hi = mid + 1
      while (k <= hi) {
        if ((t_lo <= mid) && ((t_hi > hi) || (a(t_lo) >= a(t_hi)))) {
          scratch(k) = a(t_lo)
          t_lo += 1
        } else {
          scratch(k) = a(t_hi)
          t_hi += 1
        }
        k += 1
      }
      k = lo
      while (k <= hi) {
        a(k) = scratch(k)
        k += 1
      }
    }
  }

  def scalaMergeSortPerform(a: Array[Long]) {
    scalaMergeSort(a, 0, a.length - 1, Array.ofDim[Long](a.length), (a:Long, b:Long) => a < b)
  }

  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || (al_next <= m && a(al_next) < a(am_next))) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    //Array.copy(b, l, a, l, r - l + 1)
    for (i <- l to r)
      a(i) = b(i)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    /*Arrays.sort(array)
    Arrays.sort(b)*/
    scalaMergeSortPerform(array)
    scalaMergeSortPerform(b)
    /*mergeSort(array)
    mergeSort(b)*/
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

//import scala.util.Sorting
import java.util.Arrays
object Main {
  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || (al_next <= m && a(al_next) < a(am_next))) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    Array.copy(b, l, a, l, r - l + 1)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    Arrays.sort(array)
    Arrays.sort(b)
    /*mergeSort(array)
    mergeSort(b)*/
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
object Main {
  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || (al_next <= m && a(al_next) < a(am_next))) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    Array.copy(b, l, a, l, r - l + 1)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    mergeSort(array)
    mergeSort(b)
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
object Main {
  def readLineOfNumsL = readLine split ' ' map(_.toLong) toList
  def readLineOfNumsI = readLine split ' ' map(_.toInt) toList
  def main(args: Array[String]) = {
    val List(n, q) = readLineOfNumsI
    val array = readLineOfNumsL
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val List(l, r) = readLineOfNumsI
      d(l-1) += 1
      d(r) -= 1
    }
    val b = d.tail.view.init.scanLeft(d.head) (_ + _)
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Int](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
 //   val b = d.tail.scanLeft(d.head) (_ + _)

    val b = Array.ofDim[Int](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = d.tail.view.init.scanLeft(d.head) (_ + _)
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Int](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    val sortedArray = array.sorted;
    val sortedB = b.sorted;
    var result = 0L
    for (i <- 0 to (n-1))
      result += sortedArray(i) * sortedB(i)
    println(result)
  }
}

import scala.io.StdIn

/**
  * @author traff
  */

object Main extends App {
  val Array(n, q) = StdIn.readLine().split(" ").map(_.toInt)

  val a: List[Int] = StdIn.readLine().split(" ").map(_.toInt).sortWith(_ > _).toList

  val d = Array.fill[Int](n+1)(0)

  def lsb(i: Int): Int = i & (-i)

  def update(i: Int, c: Int): Unit = {
    var ii = i

    while (ii <= n) {
      d(ii) += c
      ii += lsb(ii)
    }
  }

  def range_inc(l: Int, r: Int): Unit = {
    update(l, 1)
    update(r+1, -1)
  }

  def query(i: Int): Int = {
    var ii = i
    var sum = 0

    while (ii> 0) {
      sum+=d(ii)
      ii -= lsb(ii)
    }
    sum
  }

  for (i <- 1 to q) {
    val Array(l, r) = StdIn.readLine().split(" ").map(_.toInt)

    range_inc(l, r)
  }

  println((1 to n).map(i => query(i)).sortWith(_ > _).zip(a).foldLeft[Long](0)((r: Long, t) => r + t._1.toLong * t._2))

}

import scala.io.StdIn

/**
  * @author traff
  */

object Main extends App {
  val Array(n, q) = StdIn.readLine().split(" ").map(_.toInt)

  val a: List[Int] = StdIn.readLine().split(" ").map(_.toInt).sortWith(_ > _).toList

  val d = Array.fill[Int](n+1)(0)

  def lsb(i: Int): Int = i & (-i)

  def update(i: Int, c: Int): Unit = {
    var ii = i

    while (ii <= n) {
      d(ii) += c
      ii += lsb(ii)
    }
  }

  def range_inc(l: Int, r: Int): Unit = {
    update(l, 1)
    update(r+1, -1)
  }

  def query(i: Int): Int = {
    var ii = i
    var sum = 0

    while (ii> 0) {
      sum+=d(ii)
      ii -= lsb(ii)
    }
    sum
  }

  for (i <- 1 to q) {
    val Array(l, r) = StdIn.readLine().split(" ").map(_.toInt)

    range_inc(l, r)
  }

  println((1 to n).map(i => query(i)).sortWith(_ > _).zip(a).foldLeft(0)((r, t) => r + t._1 * t._2))

}

import scala.io.StdIn

/**
  * @author traff
  */

object Main extends App {
  val Array(n, q) = StdIn.readLine().split(" ").map(_.toInt)

  val a: List[Int] = StdIn.readLine().split(" ").map(_.toInt).sortWith(_ > _).toList

  val d = Array.fill[Int](n+1)(0)

  def lsb(i: Int): Int = i & (-i)

  def update(i: Int, c: Int): Unit = {
    var ii = i

    while (ii <= n) {
      d(ii) += c
      ii += lsb(ii)
    }
  }

  def range_inc(l: Int, r: Int): Unit = {
    update(l, 1)
    update(r+1, -1)
  }

  def query(i: Int): Int = {
    var ii = i
    var sum = 0

    while (ii> 0) {
      sum+=d(ii)
      ii -= lsb(ii)
    }
    sum
  }

  for (i <- 1 to q) {
    val Array(l, r) = StdIn.readLine().split(" ").map(_.toInt)

    range_inc(l, r)
  }

  println((1 to n).map(i => query(i)).sortWith(_ > _).zip(a).foldLeft[Long](0)((r: Long, t) => r + t._1 * t._2))

}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, q) = in.next().split(' ').map(_.toInt)
  val data = in.next().split(' ').map(_.toInt).sorted
  val frequency = Array.ofDim[Int](n)
  (1 to q).foreach { _ =>
    val Array(l, r) = in.next().split(' ').map(_.toInt - 1)
    frequency(l) += 1
    if (r != n - 1)
      frequency(r + 1) -= 1
  }
  val sortedFreq = frequency.foldLeft((List.empty[Int], 0)) {
    case((list, soFar), i) => (soFar + i :: list, soFar + i)
  }._1.sorted
  println(sortedFreq.zip(data).foldLeft(0l) {
    case(acc, (count, value)) => acc + count * value
  })

}

import scala.util.Sorting
object Main {
  def merge(a: Array[Long], b: Array[Long], l: Int, r:Int, m:Int) {
    var al_next = l
    var am_next = m + 1
    for (i <- l to r)
      if (am_next > r || a(al_next) < a(am_next)) {
        b(i) = a(al_next)
        al_next += 1
      }
      else {
        b(i) = a(am_next)
        am_next += 1
      }
  }
  def mergeSortAct(a: Array[Long], b: Array[Long], l: Int, r: Int) {
    if (l >= r)
      return
    val m = (l + r) / 2
    mergeSortAct(a, b, l, m)
    mergeSortAct(a, b, m+1, r)
    merge(a, b, l, r, m)
    Array.copy(b, l, a, l, r - l + 1)
  }
  def mergeSort(a: Array[Long]) {
    mergeSortAct(a, Array.ofDim[Long](a.length), 0, a.length - 1)
  }
  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Long](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    mergeSort(array)
    mergeSort(b)
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsI()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Int](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    Sorting.quickSort(array)
    Sorting.quickSort(b)
    var result = 0
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = d.scanLeft(0)(_ + _)
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = Array.ofDim[Int](n)
    var count = 0
    for (i <- 0 to (n-1)) {
      count += d(i)
      b(i) = count
    }
    val sortedArray = array.sorted;
    val sortedB = b.sorted;
    var result = 0L
    for (i <- 0 to (n-1))
      result += array(i) * b(i)
    println(result)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = d.tail.scanLeft(d.head) (_ + _)
    println((array.sorted, b.sorted).zipped map (_ * _) sum)
  }
}

import scala.util.Sorting
object Main {

  def readLineOfNumsL(): Array[Long] = readLine().split(" ").map(_.toLong)
  def readLineOfNumsI(): Array[Int] = readLine().split(" ").map(_.toInt)
  def main(args: Array[String]) = {
    val Array(n, q) = readLineOfNumsI()
    val array = readLineOfNumsL()
    val d = Array.ofDim[Int](n+1)
    for (i <- 1 to q) {
      val Array(l, r) = readLineOfNumsI()
      d(l-1) += 1
      d(r) -= 1
    }
    val b = d.tail.scanLeft(d.head) (_ + _)
    println((array.sorted, b.sorted).zipped.map(_ * _).sum)
  }
}


import scala.collection.mutable

object C {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n = nextInt
    val as = nextLongs(n)

    val resBuilder = new mutable.ArrayBuilder.ofLong
    var prev = 0L
    for (a <- as) {
      if (java.lang.Long.signum(a) != java.lang.Long.signum(prev)) {
        if (prev != 0) {
          resBuilder += prev
        }
        prev = a
      } else if (a > prev) {
        prev = a
      }
    }
    if (prev != 0) {
      resBuilder += prev
    }

    out.println(resBuilder.result().sum)
  }

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

import scala.io.StdIn

object SolutionC extends App {
  val T = StdIn.readInt()
  for (t <- 1 to T) {
    val n = StdIn.readInt()
    val array = StdIn.readLine().split(' ').map{_.toInt}
    var prev = array(0)
    var currentMax = prev
    var result = 0L
    for (idx <- 1 until n) {
      val next = array(idx)
      if (prev < 0) {
        if (next < 0) {
          currentMax = Math.max(currentMax, next)
        } else {
          result += currentMax
          currentMax = next
        }
      } else {
        if (next > 0) {
          currentMax = Math.max(currentMax, next)
        } else {
          result += currentMax
          currentMax = next
        }
      }
      prev = next
    }
    result += currentMax
    println(result)
  }
}

import java.util.Scanner

object C_1343 extends App {
  var scanner = new Scanner(System.in)
  val t = scanner.nextInt()

  (0 until t).foreach { _ =>
    val n = scanner.nextInt()

    val a0 = scanner.nextInt()
    var sign = a0 > 0
    var max = a0
    var sum = 0L

    var index = 1
    while (index < n) {
      val newA = scanner.nextInt()
      val newSign = newA > 0
      if (newSign != sign) {
        sign = newSign
        sum += max
        max = newA
      }
      else if (newA > max) {
        max = newA
      }

      index += 1
    }
    sum += max

    println(sum)
  }
}


object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline private final def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline private final def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    var i = from
    while(i <= to) { f(i); i += 1 }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    val n, m = ni()
    val g = Array.fill[ArrayBuffer[Int]](n)(ArrayBuffer())
    val degIn, degOut = Array.ofDim[Int](n)

    def addEdge(from: Int, to: Int): Unit = {
      g(from) += to
      degIn(from) += 1
      degOut(to) += 1
    }

    REP(m) { _ =>
      val a, b = ni() - 1
      // bragされる側からの有向グラフ
      if (a < b) addEdge(a, b) else addEdge(b, a)
    }

    var ans = 0L
    REP(n) { v =>
      ans += degIn(v).toLong * degOut(v)
    }
    out.println(ans)
    REP(ni()) { _ =>
      val v = ni() - 1
      val prev = degOut(v).toLong * degIn(v)
      degOut(v) += degIn(v)
      degIn(v) = 0
      ans -= prev
      var i = 0
      while (i < g(v).length) {
        val u = g(v)(i)
        g(u) += v
        val prev = degIn(u).toLong * degOut(u)
        degIn(u) += 1
        degOut(u) -= 1
        val cur = degIn(u).toLong * degOut(u)
        ans += cur - prev
        i += 1
      }
      g(v).clear()
      out.println(ans)
    }
  }
}

object Main {

  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer

  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null

  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj) {
      f
    }
  }

  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj) {
    debug(as.map(x => if (x) "1" else "0").mkString)
  }

  @inline private final def debug(as: Array[Int]): Unit = if (!oj) {
    debug(as.mkString(" "))
  }

  @inline private final def debug(as: Array[Long]): Unit = if (!oj) {
    debug(as.mkString(" "))
  }

  @inline private final def debugDim(m: Array[Array[Long]]): Unit = if (!oj) {
    REP(m.length) { i =>
      debug(m(i))
    }
  }

  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj) {
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }

  @inline private final def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }

  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i); i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i); i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    var i = from
    while (i <= to) {
      f(i); i += 1
    }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {

  import java.util.Arrays.sort

  import Main._
  import sc._

  import scala.collection.mutable
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    val n, m = ni()
    val g = Array.fill[ArrayBuffer[Int]](n)(ArrayBuffer())
    val degIn, degOut = Array.ofDim[Long](n)

    def addEdge(from: Int, to: Int): Unit = {
      g(from) += to
      degIn(from) += 1
      degOut(to) += 1
    }

    REP(m) { _ =>
      val a, b = ni() - 1
      // bragされる側からの有向グラフ
      if (a < b) addEdge(a, b) else addEdge(b, a)
    }

    var ans = 0L
    REP(n) { v =>
      ans += degIn(v) * degOut(v)
    }
    out.println(ans)
    REP(ni()) { _ =>
      val v = ni() - 1
      val prev = degOut(v) * degIn(v)
      degOut(v) += degIn(v)
      degIn(v) = 0
      ans -= prev
      var i = 0
      val len = g(v).length
      while (i < len) {
        val u = g(v)(i)
        g(u) += v
        val prev = degIn(u) * degOut(u)
        degIn(u) += 1
        degOut(u) -= 1
        val cur = degIn(u) * degOut(u)
        ans += cur - prev
        i += 1
      }
      g(v).clear()
      out.println(ans)
    }
  }
}

object Main {

  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer

  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null

  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj) {
      f
    }
  }

  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj) {
    debug(as.map(x => if (x) "1" else "0").mkString)
  }

  @inline private final def debug(as: Array[Int]): Unit = if (!oj) {
    debug(as.mkString(" "))
  }

  @inline private final def debug(as: Array[Long]): Unit = if (!oj) {
    debug(as.mkString(" "))
  }

  @inline private final def debugDim(m: Array[Array[Long]]): Unit = if (!oj) {
    REP(m.length) { i =>
      debug(m(i))
    }
  }

  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj) {
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }

  @inline private final def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }

  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i); i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i); i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    var i = from
    while (i <= to) {
      f(i); i += 1
    }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {

  import java.util.Arrays.sort

  import Main._
  import sc._

  import scala.collection.mutable
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    val n, m = ni()
    val g = Array.fill[ArrayBuffer[Integer]](n)(ArrayBuffer())
    val degIn, degOut = Array.ofDim[Long](n)

    def addEdge(from: Int, to: Int): Unit = {
      g(from) += to
      degIn(from) += 1
      degOut(to) += 1
    }

    REP(m) { _ =>
      val a, b = ni() - 1
      // bragされる側からの有向グラフ
      if (a < b) addEdge(a, b) else addEdge(b, a)
    }

    var ans = 0L
    REP(n) { v =>
      ans += degIn(v) * degOut(v)
    }
    out.println(ans)
    REP(ni()) { _ =>
      val v = ni() - 1
      val prev = degOut(v) * degIn(v)
      degOut(v) += degIn(v)
      degIn(v) = 0
      ans -= prev
      val vInt: Integer = v // boxing対策
      var i = 0
      while (i < g(v).length) {
        val u = g(v)(i).intValue()
        g(u) += vInt
        val prev = degIn(u) * degOut(u)
        degIn(u) += 1
        degOut(u) -= 1
        val cur = degIn(u) * degOut(u)
        ans += cur - prev
        i += 1
      }
      g(v).clear()
      out.println(ans)
    }
  }
}


import java.util.Scanner
import java.util.Arrays

object Solution {
    def main(args: Array[String]) = {
	val in = new Scanner(System.in)
	val n = in.nextInt()
	val m = in.nextInt()
	val b: Array[Array[Int]] = new Array(n)
	var i = 0
	for (i <- 0 until n) {
	    b(i) = new Array(m)
	    var j = 0
	    for (j <- 0 until m)
		b(i)(j) = in.nextInt()
	}
	val a: Array[Array[Int]] = new Array(n)
	for (i <- 0 until n) {
	    a(i) = new Array(m)
	    Arrays.fill(a(i), 1)
	}
	for (i <- 0 until n) {
	    var j = 0
	    for (j <- 0 until m) {
		if (b(i)(j) == 0) {
		    var k = 0
		    for (k <- 0 until m)
			a(i)(k) = 0
		    for (k <- 0 until n)
			a(k)(j) = 0
		}
	    }
	}
	var ok = true
	for (i <- 0 until n) {
	var j = 0
	    for (j <- 0 until m) {
		var curr = 0
		var k = 0
		for (k <- 0 until m)
		    curr |= a(i)(k)
		for (k <- 0 until n)
		    curr |= a(k)(j)
		if (curr != b(i)(j))
		    ok = false
	    }
	}
	if (ok) {
	    println("YES")
	    for (i <- 0 until n) {
		var j = 0
		for (j <- 0 until m)
		    print(a(i)(j) + " ")
		println()
	    }
	} else {
	    println("NO")
	}
    }
}

import scala.io.StdIn._

object ProblemB {
  def main(args: Array[String]) {
    val Array(m, n) = readLine split " " map (_.toInt)
    var B = new Array[Array[Int]](m)
    for (i <- 0 until m) {
      B(i) = readLine split " " map (_.toInt)
    }

    var A = Array.tabulate(m, n)((x, y) => 1)
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if (B(i)(j) == 0) {
          for (k <- 0 until m) A(k)(j) = 0
          for (k <- 0 until n) A(i)(k) = 0
        }
      }
    }

    val rows = for (i <- 0 until m) yield { A(i) exists (_ == 1) }
    val cols = for (j <- 0 until n) yield {
      (for (i <- 0 until m) yield A(i)(j)) exists (_ == 1)
    }

    var win = true
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if ((B(i)(j) == 1) != (rows(i) || cols(j)))
          win = false
      }
    }

    if (!win) {
      println("NO")
    } else {
      println("YES")
      for (i <- 0 until m) {
        println(A(i) mkString " ")
      }
    }

  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, n) = in.next().split(' ').map(_.toInt)
  val sourceMatrix = Array.ofDim[Int](m, n)
  val emptyRow = Array.ofDim[Boolean](m)
  val emptyCol = Array.ofDim[Boolean](n)
  (0 until m).foreach{i =>
    val row = in.next().split(' ').map(_.toInt)
    sourceMatrix(i) = row
    val index = row.indexOf(0)
    if (index != -1) {
      emptyRow(i) = true
      (index until n).foreach { i => emptyCol(i) ||= row(i) == 0 }
    }
  }

  val res = (0 until m).forall {i =>
    (0 until n).forall { j =>
      val cell = emptyCol(j) && emptyRow(i)
      if (cell)
        sourceMatrix(i)(j) == 0
      else
        sourceMatrix(i)(j) == 1
      }
    }
  val fillRows = emptyRow.count(_ == false)
  val fillCols = emptyCol.count(_ == false)

  if (res && (fillRows == fillCols || (fillRows != 0 && fillCols != 0))) {
    println("YES")
    println((0 until m).map {i =>
      (0 until n).map { j =>
        if (emptyRow(i) || emptyCol(j)) 0 else 1
      }.mkString(" ")
    }.mkString("\n"))
  } else
    println("NO")

}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, n) = in.next().split(' ').map(_.toInt)
  val sourceMatrix = Array.ofDim[Int](m, n)
  val emptyRow = Array.ofDim[Boolean](m)
  val emptyCol = Array.ofDim[Boolean](n)
  (0 until m).foreach{i =>
    val row = in.next().split(' ').map(_.toInt)
    sourceMatrix(i) = row
    val index = row.indexOf(0)
    if (index != -1) {
      emptyRow(i) = true
      (index until n).foreach { i => emptyCol(i) ||= row(i) == 0 }
    }
  }

  val res = (0 until m).forall {i =>
    (0 until n).forall { j =>
      (sourceMatrix(i)(j) == 1) ||
        sourceMatrix(i).contains(1) || (0 until m).exists(i => sourceMatrix(i)(j) == 1)
      }
    }

  if (res) {
    println("YES")
    println((0 until m).map {i =>
      (0 until n).map { j =>
        if (emptyRow(i) || emptyCol(j)) 0 else 1
      }.mkString(" ")
    }.mkString("\n"))
  } else
    println("NO")

}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, n) = in.next().split(' ').map(_.toInt)
  val sourceMatrix = Array.ofDim[Int](m, n)
  val emptyRow = Array.ofDim[Boolean](m)
  val emptyCol = Array.ofDim[Boolean](n)
  (0 until m).foreach{i =>
    val row = in.next().split(' ').map(_.toInt)
    sourceMatrix(i) = row
    val index = row.indexOf(0)
    if (index != -1) {
      emptyRow(i) = true
      (index until n).foreach { i => emptyCol(i) ||= row(i) == 0 }
    }
  }

  val res = (0 until m).forall {i =>
    (0 until n).forall { j =>
      val cell = emptyCol(j) && emptyRow(i)
      if (sourceMatrix(i)(j) == 1)
        !cell
      else
        cell
      }
    }

  if (res) {
    println("YES")
    println((0 until m).map {i =>
      (0 until n).map { j =>
        if (emptyRow(i) || emptyCol(j)) 0 else 1
      }.mkString(" ")
    }.mkString("\n"))
  } else
    println("NO")

}

import scala.io.StdIn._

object ProblemB {
  def main(args: Array[String]) {
    val Array(m, n) = readLine split " " map (_.toInt)
    var B = new Array[Array[Int]](m)
    for (i <- 0 until m) {
      B(i) = readLine split " " map (_.toInt)
    }

    var rows = Array.fill(m)(1)
    var cols = Array.fill(n)(1)
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if (B(i)(j) == 0) {
          rows(i) = 0
          cols(j) = 0
        }
      }
    }

    var win = true
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if (B(i)(j) != (rows(i) & cols(j)))
          win = false
      }
    }

    val row = for (j <- 0 until n) yield j

    if (!win) {
      println("NO")
    } else {
      println("YES")
      for (i <- 0 until m) {
        println(row map (cols(_) & rows(i)) mkString " ")
      }
    }
  }
}

import scala.io.StdIn._

object ProblemB {
  def main(args: Array[String]) {
    val Array(m, n) = readLine split " " map (_.toInt)
    var B = new Array[Array[Int]](m)
    for (i <- 0 until m) {
      B(i) = readLine split " " map (_.toInt)
    }

    var rows = Array.fill(m)(1)
    var cols = Array.fill(n)(1)
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if (B(i)(j) == 0) {
          rows(i) = 0
          cols(j) = 0
        }
      }
    }

    var win = true
    for (i <- 0 until m) {
      for (j <- 0 until n) {
        if (B(i)(j) != (rows(i) | cols(j)))
          win = false
      }
    }

    val row = for (j <- 0 until n) yield j

    if (!win) {
      println("NO")
    } else {
      println("YES")
      for (i <- 0 until m) {
        println(row map (cols(_) & rows(i)) mkString " ")
      }
    }
  }
}