#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n, long long p = P) {
  long long r = 1 % p;
  for (a %= P; n; a = a * a % p, n >>= 1)
    if (n & 1) r = r * a % p;
  return r;
}
long long inv(long long x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
const int N = 1e6 + 10;
long long C[N];
int main() {
  int n;
  scanf("%d", &n);
  C[0] = 1;
  for (int i = 1; i <= n; ++i) C[i] = C[i - 1] * (n - i + 1) % P * inv(i) % P;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int t = C[i] * qpow(3, (long long)n * n - (long long)n * i + i) % P;
    if (i & 1)
      ans += t;
    else
      ans += P - t;
  }
  ans = ans % P * 2 % P;
  long long ans1 = 0;
  for (int i = 1; i <= n; ++i) {
    int t = C[i] * inv(qpow(3, (long long)n * i)) % P *
            (qpow(1 + P - inv(qpow(3, n - i)), n) % P + P - 1) % P;
    if (i & 1)
      ans1 += t;
    else
      ans1 += P - t;
  }
  ans1 = ans1 % P * qpow(3, (long long)n * n + 1) % P;
  printf("%d\n", int((ans1 + ans) % P));
}
