A compressed array

4.3

3 votes
Counting and Arrangements, Dynamic Programming, Algorithms
Problem

You are given an array a of size n. The compressed version of an array is defined as replacing the occurance of consecutive equal integers with the single occurance.

Example: For a=[1,1,2,2,1,1], the compressed version will be [1,2,1].

The compressed number of an array is defined as number of ways to remove some non-empty subsequence of indexes from the initial array to make it compressed. Two ways are considered different if the sequence of indexes deleted from the array was different, the order of removing does not matter.

Calculate the sum of compressed number for all subarrays of the array a. Since the sum may be large, print it modulo 109+7.

Example: For a=[3,3,3,3], the subarrays with the compressed numbers are as follows: 

  • There are 4 subarrays of type [3]. The compressed number is 0 as it is already compressed.
  • There are 3 subarrays of type a=[3,3]. The compressed number is 2 as either of the index can be deleted. So, the sum of compressed number would be 6.
  • There are 2 subarrays of type a=[3,3,3]. the compressed number is 3 as either of the sequence of indices [1,2],[1,3] or [2,3] can be deleted. So, the sum of compressed number would be 6.
  • There is 1 subarray [3,3,3,3] with the compressed number 4.
  • So, the sum of compressed number of all subarrays is 16.

Input format  

  • The first line contains an integer t denoting the number of test cases. 
  • The first line of each test case contains an integer n denoting the size of the array a.
  • The second line of each test case contains n space-separated integers denoting the elements of array a.

Output format

Print a single integer for each test case denoting the sum of compressed number for all the subarrays of the array a.

Constraints

1t101n1051a[i]105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For test case 1 : 

We have a=[6,7,3,6,6].. The subarrays of a with their respective compressed numbers are as follows : 

[6] has compressed number 0 as it is already compressed.

Similarly, [6,7],[6,7,3],[6,7,3,6] have compressed number 0 as they are already compressed.

[6,7,3,6,6] has compressed number 2 as we can delete either index 4 or index 5 to get compressed version [6,7,3,6].

[7],[7,3],[7,3,6] have compressed number 0 as they are already compressed.

[7,3,6,6] has compressed number 2.

[3],[3,6] have compressed number 0 as they are already compressed.

[3,6,6] has compressed number 2.

[6] has compressed number 0 as it is already compressed.

[6,6] has compressed number 2.

[6] has compressed number 0 as it is already compressed.

So, total sum of all compressed numbers of all subarrays of a = 8.

For test case 2 : 

We have a=[4,4].. The subarrays of a with their respective compressed numbers are as follows : 

[4] has compressed number 0 as it is already compressed..

[4,4] has compressed number 2 as we can delete either index 1 or index 2 to get compressed version [4].

So, sum of compressed number = 2.

Editor Image

?