#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
const int mod = 998244353;
int f[N], inv[N];
void init() {
  f[0] = f[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i < N; i++) {
    f[i] = 1LL * f[i - 1] * i % mod;
    inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
  }
  for (int i = 2; i < N; i++) inv[i] = 1LL * inv[i] * inv[i - 1] % mod;
}
int c(int n, int m) {
  if (n < m) return 0;
  return (1LL * f[n] * inv[m] % mod) * inv[n - m] % mod;
}
int qp(int a, long long b) {
  int res = 1;
  while (b) {
    if (b & 1LL) res = 1LL * res * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  int n;
  init();
  scanf("%d", &n);
  int res = 0;
  int sign = 1;
  for (int i = 1; i <= n; i++) {
    int a = 1LL * sign * c(n, i) % mod;
    int x = 1LL * qp(3, (1LL * (n - i) * n + i) % (mod - 1));
    res = (res + 1LL * a * x % mod) % mod;
    sign = mod - sign;
  }
  res = 2LL * res % mod;
  sign = mod - 1;
  for (int i = 0; i < n; i++) {
    int val = mod - qp(3, i);
    int x = qp(val + 1, n);
    x = (x - qp(val, n) + mod) % mod;
    int a = 3LL * sign * c(n, i) % mod;
    x = 1LL * a * x % mod;
    res = (res + x) % mod;
    sign = mod - sign;
  }
  printf("%d\n", res);
}
